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.5 by tdb, Tue May 21 16:47:12 2002 UTC

# Line 1 | Line 1
1   #!/usr/bin/perl -w
2  
3 + #
4 + # i-scream central monitoring system
5 + # http://www.i-scream.org.uk
6 + # Copyright (C) 2000-2002 i-scream
7 + #
8 + # This program is free software; you can redistribute it and/or
9 + # modify it under the terms of the GNU General Public License
10 + # as published by the Free Software Foundation; either version 2
11 + # of the License, or (at your option) any later version.
12 + #
13 + # This program is distributed in the hope that it will be useful,
14 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 + # GNU General Public License for more details.
17 + #
18 + # You should have received a copy of the GNU General Public License
19 + # along with this program; if not, write to the Free Software
20 + # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 + #
22 +
23   #-----------------------------------------------------------------
24   # i-scream host plugin - uptime & load calculation
25   #     $Author$
# Line 15 | Line 35 | $| = 1;
35   # You'd be silly not to use this ;)
36   use strict;
37  
38 < # Have to hope this will work really.
39 < my($ostype) = `uname -s`; chomp($ostype);
38 > # Get the OS type from the args, or try towork it out
39 > my($ostype) = $ARGV[0];
40 > $ostype = `uname -s` if not defined $ostype;
41 > chomp($ostype);
42  
43   # Decide which paths we should use.
44 < my($uptimebin);
45 < if ($ostype eq "SunOS" || $ostype eq "Linux" || $ostype eq "FreeBSD") {
44 > my($uptimebin); my($sysctlbin);
45 > if ($ostype eq "SunOS" || $ostype eq "Linux") {
46      # covers: Solaris 7/8
47      # covers: Debian r2.2
26    # covers: FreeBSD 4.X
48      $uptimebin = "/usr/bin/uptime";
49   }
50 + elsif ($ostype eq "FreeBSD") {
51 +    # covers: FreeBSD 4.X
52 +    $sysctlbin = "/sbin/sysctl";
53 + }
54   else {
55      print "i-scream_uptime.pl Error: Unable to identify system type - \"$ostype\".\n";
56      print "\"uname -s\" does not report one of the following known types;\n";
# Line 34 | Line 59 | else {
59   }
60  
61   # Run the following components: -
37 &print_ident();
62   &include_uptime();
63  
64   # End the program normally.
# Line 43 | Line 67 | exit(0);
67  
68  
69  
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
70   # sub to print pairs of data, separated by a single space character.
71   # If the second argument is undefined, then the pair is still printed,
72   # however, the value shall be displayed as the the 'default' value
# Line 71 | Line 88 | sub print_pair($$$) {
88   # get system uptime in seconds, and the current load
89   sub include_uptime() {
90  
91 <    # grab the uptime
92 <    my($uptime) = `$uptimebin`;
91 >    # if it's FreeBSD, get the uptime and load sanely :)
92 >    if ($ostype eq "FreeBSD") {
93 >        my($boottime) = `$sysctlbin -n kern.boottime`;
94 >        if($boottime =~ /^{ sec = (\d+),/) {
95 >            &print_pair("unknown", "packet.os.uptime", time()-$1);
96 >        }
97      
98 <    # grab the load
99 <    &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
100 <    &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
101 <    &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
102 <
103 <    # 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";
98 >        my($loadavg) = `$sysctlbin -n vm.loadavg`;
99 >        if($loadavg =~ /\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+/) {
100 >            &print_pair(0, "packet.load.load1", $1);
101 >            &print_pair(0, "packet.load.load5", $2);
102 >            &print_pair(0, "packet.load.load15", $3);
103 >        }
104      }
105 +    
106 +    # otherwise, lets parse :)
107      else {
108 <        if ($uptime =~ /day/) {
109 <            if ($uptime =~ /hr/) {
110 <              # 0 minutes
111 <                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
112 <                $uptime = "$1:$2:0";
108 >        # grab the uptime
109 >        my($uptime) = `$uptimebin`;
110 >        
111 >        # grab the load
112 >        &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
113 >        &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
114 >        &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
115 >    
116 >        # work out the days, hours, and minutes
117 >    
118 >        if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
119 >          # normal
120 >            $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
121 >            $uptime = "$1:$2:$3";
122 >        }
123 >        else {
124 >            if ($uptime =~ /day/) {
125 >                if ($uptime =~ /hr/) {
126 >                  # 0 minutes
127 >                    $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
128 >                    $uptime = "$1:$2:0";
129 >                }
130 >                elsif ($uptime =~ /min/) {
131 >                  # 0 hours
132 >                    $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
133 >                    $uptime = "$1:0:$2";
134 >                }
135 >                else {
136 >                  # 0 hours and 0 mins
137 >                    $uptime =~ /up\s+([0-9]+)/;
138 >                    $uptime = "$1:0:0";
139 >                }
140              }
141 +            elsif ($uptime =~ /hr/) {
142 +              # 0 days and 0 minutes
143 +                $uptime =~ /up\s+([0-9]+)\s+/;
144 +                $uptime = "0:$1:0";
145 +            }
146              elsif ($uptime =~ /min/) {
147 <              # 0 hours
148 <                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
149 <                $uptime = "$1:0:$2";
147 >              # 0 days and 0 hours
148 >                $uptime =~ /up\s+([0-9]+)\s+/;
149 >                $uptime = "0:0:$1";
150              }
151              else {
152 <              # 0 hours and 0 mins
153 <                $uptime =~ /up\s+([0-9]+)/;
154 <                $uptime = "$1:0:0";
152 >              # 0 days
153 >                $uptime =~ /up\s+([0-9]+):([0-9]+)/;
154 >                $uptime = "0:$1:$2";
155              }
156          }
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;
157      
158 <    # print the value out
159 <    &print_pair("unknown", "packet.os.uptime", $uptime);
158 >        # turn into seconds
159 >        $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
160 >        $uptime = ($3+($2+($1*24))*60)*60;
161 >        
162 >        # print the value out
163 >        &print_pair("unknown", "packet.os.uptime", $uptime);
164 >    }
165  
166   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines