ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/perl/i-scream_uptime.pl
(Generate patch)

Comparing projects/cms/source/host/ihost-perl/plugins/perl/i-scream_uptime.pl (file contents):
Revision 1.1 by tdb, Mon Nov 19 21:42:11 2001 UTC vs.
Revision 1.3 by tdb, Tue Feb 12 17:14:56 2002 UTC

# Line 15 | Line 15 | $| = 1;
15   # You'd be silly not to use this ;)
16   use strict;
17  
18 < # Have to hope this will work really.
19 < my($ostype) = `uname -s`; chomp($ostype);
18 > # Get the OS type from the args, or try towork it out
19 > my($ostype) = $ARGV[0];
20 > $ostype = `uname -s` if not defined $ostype;
21 > chomp($ostype);
22  
23   # Decide which paths we should use.
24 < my($uptimebin);
25 < if ($ostype eq "SunOS" || $ostype eq "Linux" || $ostype eq "FreeBSD") {
24 > my($uptimebin); my($sysctlbin);
25 > if ($ostype eq "SunOS" || $ostype eq "Linux") {
26      # covers: Solaris 7/8
27      # covers: Debian r2.2
26    # covers: FreeBSD 4.X
28      $uptimebin = "/usr/bin/uptime";
29   }
30 + elsif ($ostype eq "FreeBSD") {
31 +    # covers: FreeBSD 4.X
32 +    $sysctlbin = "/sbin/sysctl";
33 + }
34   else {
35      print "i-scream_uptime.pl Error: Unable to identify system type - \"$ostype\".\n";
36      print "\"uname -s\" does not report one of the following known types;\n";
# Line 34 | Line 39 | else {
39   }
40  
41   # Run the following components: -
37 &print_ident();
42   &include_uptime();
43  
44   # End the program normally.
# Line 43 | Line 47 | exit(0);
47  
48  
49  
46 # prints out an identifier for this version of the script
47 # this could be used in checks further downstream
48 sub print_ident() {
49    print 'packet.plugins.ident.i-scream_uptime i-scream_uptime.pl $Revision$';
50    print "\n";
51 }
52
50   # sub to print pairs of data, separated by a single space character.
51   # If the second argument is undefined, then the pair is still printed,
52   # however, the value shall be displayed as the the 'default' value
# Line 71 | Line 68 | sub print_pair($$$) {
68   # get system uptime in seconds, and the current load
69   sub include_uptime() {
70  
71 <    # grab the uptime
72 <    my($uptime) = `$uptimebin`;
71 >    # if it's FreeBSD, get the uptime and load sanely :)
72 >    if ($ostype eq "FreeBSD") {
73 >        my($boottime) = `$sysctlbin -n kern.boottime`;
74 >        if($boottime =~ /^{ sec = (\d+),/) {
75 >            &print_pair("unknown", "packet.os.uptime", time()-$1);
76 >        }
77      
78 <    # grab the load
79 <    &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
80 <    &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
81 <    &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
82 <
83 <    # work out the days, hours, and minutes
83 <
84 <    if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
85 <      # normal
86 <        $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
87 <        $uptime = "$1:$2:$3";
78 >        my($loadavg) = `$sysctlbin -n vm.loadavg`;
79 >        if($loadavg =~ /\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+/) {
80 >            &print_pair(0, "packet.load.load1", $1);
81 >            &print_pair(0, "packet.load.load5", $2);
82 >            &print_pair(0, "packet.load.load15", $3);
83 >        }
84      }
85 +    
86 +    # otherwise, lets parse :)
87      else {
88 <        if ($uptime =~ /day/) {
89 <            if ($uptime =~ /hr/) {
90 <              # 0 minutes
91 <                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
92 <                $uptime = "$1:$2:0";
88 >        # grab the uptime
89 >        my($uptime) = `$uptimebin`;
90 >        
91 >        # grab the load
92 >        &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
93 >        &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
94 >        &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
95 >    
96 >        # work out the days, hours, and minutes
97 >    
98 >        if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
99 >          # normal
100 >            $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
101 >            $uptime = "$1:$2:$3";
102 >        }
103 >        else {
104 >            if ($uptime =~ /day/) {
105 >                if ($uptime =~ /hr/) {
106 >                  # 0 minutes
107 >                    $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
108 >                    $uptime = "$1:$2:0";
109 >                }
110 >                elsif ($uptime =~ /min/) {
111 >                  # 0 hours
112 >                    $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
113 >                    $uptime = "$1:0:$2";
114 >                }
115 >                else {
116 >                  # 0 hours and 0 mins
117 >                    $uptime =~ /up\s+([0-9]+)/;
118 >                    $uptime = "$1:0:0";
119 >                }
120              }
121 +            elsif ($uptime =~ /hr/) {
122 +              # 0 days and 0 minutes
123 +                $uptime =~ /up\s+([0-9]+)\s+/;
124 +                $uptime = "0:$1:0";
125 +            }
126              elsif ($uptime =~ /min/) {
127 <              # 0 hours
128 <                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
129 <                $uptime = "$1:0:$2";
127 >              # 0 days and 0 hours
128 >                $uptime =~ /up\s+([0-9]+)\s+/;
129 >                $uptime = "0:0:$1";
130              }
131              else {
132 <              # 0 hours and 0 mins
133 <                $uptime =~ /up\s+([0-9]+)/;
134 <                $uptime = "$1:0:0";
132 >              # 0 days
133 >                $uptime =~ /up\s+([0-9]+):([0-9]+)/;
134 >                $uptime = "0:$1:$2";
135              }
136          }
107        elsif ($uptime =~ /hr/) {
108          # 0 days and 0 minutes
109            $uptime =~ /up\s+([0-9]+)\s+/;
110            $uptime = "0:$1:0";
111        }
112        elsif ($uptime =~ /min/) {
113          # 0 days and 0 hours
114            $uptime =~ /up\s+([0-9]+)\s+/;
115            $uptime = "0:0:$1";
116        }
117        else {
118          # 0 days
119            $uptime =~ /up\s+([0-9]+):([0-9]+)/;
120            $uptime = "0:$1:$2";
121        }
122    }
123
124    # turn into seconds
125    $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
126    $uptime = ($3+($2+($1*24))*60)*60;
137      
138 <    # print the value out
139 <    &print_pair("unknown", "packet.os.uptime", $uptime);
138 >        # turn into seconds
139 >        $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
140 >        $uptime = ($3+($2+($1*24))*60)*60;
141 >        
142 >        # print the value out
143 >        &print_pair("unknown", "packet.os.uptime", $uptime);
144 >    }
145  
146   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines