]> git.zerfleddert.de Git - upsgraph/blobdiff - upsgraph.pl
change uid when invoked as root
[upsgraph] / upsgraph.pl
index 745a44cbe2dae92c4a3b7d3ea17bbab1417a35e1..bef10a5460a9d302b8eb974c630b7e1ffec577ec 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl -w
 
-if (@ARGV != 3) {
-       print STDERR "Syntax: ${0} host /path/to/file.rrd /output/directory/\n";
+if ((@ARGV != 3) && (@ARGV != 4)) {
+       print STDERR "Syntax: ${0} host /path/to/file.rrd /output/directory/ [uid]\n";
        exit(1);
 }
 
@@ -97,6 +97,16 @@ my $vars = {
 
 use Net::SNMP;
 
+if ($> == 0) {
+       if (@ARGV != 4) {
+               print STDERR "Running as root, please provide UID as 4th argument!\n";
+               exit(1);
+       }
+
+       print "Running as root, switching to ".$ARGV[3]."\n";
+       $< = $> = $ARGV[3];
+}
+
 if (! -e "${rrdfile}") {
        my $cmd = "rrdtool create \"${rrdfile}\" ";
        foreach my $var (@fields) {
@@ -110,6 +120,12 @@ if (! -e "${rrdfile}") {
        print `${cmd}`;
 }
 
+my $child = fork();
+
+die "fork failed!" if (!defined($child));
+
+exit 0 if ($child != 0);
+
 while(1) {
        ($session,$error) = Net::SNMP->session(Hostname => $host,
                        Community => $community);
@@ -137,7 +153,6 @@ while(1) {
                if (!(defined($vars->{$var}->{'value'}))) {
                        $vars->{$var}->{'value'} = 'U';
                }
-               print $vars->{$var}->{'name'}.": ".$vars->{$var}->{'value'}."\n";
                $cmd .= ":" . $vars->{$var}->{'value'};
        }
        $cmd .= "\"";
@@ -164,6 +179,7 @@ while(1) {
 
        print HTML "</body></html>\n";
        print HTML "<br>Generated on: " . localtime(time());
+       print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';
 
        close(HTML);
 
Impressum, Datenschutz