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.4 by tdb, Sat May 18 18:15:57 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines