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
Revision: 1.3
Committed: Tue Feb 12 17:14:56 2002 UTC (22 years, 8 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +71 -50 lines
Log Message:
Decided to get the FreeBSD uptime/load information in a more sane manner.
It's much quicker than parsing uptime, and more precise.

File Contents

# User Rev Content
1 tdb 1.1 #!/usr/bin/perl -w
2    
3     #-----------------------------------------------------------------
4     # i-scream host plugin - uptime & load calculation
5 tdb 1.2 # $Author: tdb $
6 tdb 1.3 # $Id: i-scream_uptime.pl,v 1.2 2001/12/18 04:07:17 tdb Exp $
7 tdb 1.1 #
8     # A short perl script to calculate the uptime in seconds,
9     # and to get the current system loads.
10     #-----------------------------------------------------------------
11    
12    
13     $| = 1;
14    
15     # You'd be silly not to use this ;)
16     use strict;
17    
18 tdb 1.2 # 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 tdb 1.1
23     # Decide which paths we should use.
24 tdb 1.3 my($uptimebin); my($sysctlbin);
25     if ($ostype eq "SunOS" || $ostype eq "Linux") {
26 tdb 1.1 # covers: Solaris 7/8
27     # covers: Debian r2.2
28 tdb 1.3 $uptimebin = "/usr/bin/uptime";
29     }
30     elsif ($ostype eq "FreeBSD") {
31 tdb 1.1 # covers: FreeBSD 4.X
32 tdb 1.3 $sysctlbin = "/sbin/sysctl";
33 tdb 1.1 }
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";
37     print " SunOS, Linux, FreeBSD\n";
38     exit(1);
39     }
40    
41     # Run the following components: -
42     &include_uptime();
43    
44     # End the program normally.
45     exit(0);
46    
47    
48    
49    
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
53     # if the passed value was undefined.
54     sub print_pair($$$) {
55     my($default, $name, $value) = @_;
56    
57     if (!defined $value) {
58     $value = $default;
59     }
60    
61     # Remove the trailing linefeed if we've not already done so.
62     chomp($value);
63    
64     # print the pair of data with a space inbetween.
65     print "$name $value\n";
66     }
67    
68     # get system uptime in seconds, and the current load
69     sub include_uptime() {
70    
71 tdb 1.3 # 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 tdb 1.1
78 tdb 1.3 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 tdb 1.1 }
85 tdb 1.3
86     # otherwise, lets parse :)
87 tdb 1.1 else {
88 tdb 1.3 # 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 tdb 1.1 }
126     elsif ($uptime =~ /min/) {
127 tdb 1.3 # 0 days and 0 hours
128     $uptime =~ /up\s+([0-9]+)\s+/;
129     $uptime = "0:0:$1";
130 tdb 1.1 }
131     else {
132 tdb 1.3 # 0 days
133     $uptime =~ /up\s+([0-9]+):([0-9]+)/;
134     $uptime = "0:$1:$2";
135 tdb 1.1 }
136     }
137 tdb 1.3
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 tdb 1.1 }
145    
146     }