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.2
Committed: Tue Dec 18 04:07:17 2001 UTC (24 years ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.1: +6 -12 lines
Log Message:
Solaris process accounting made me notice the number of times `uname` was
being called. It was way over the top, especially as it's unlikely to
change during an invocation of ihost :) Before it was run by each plugin
every time they were run - in total 50 times a minute :/ Now, it's run once
by ihost at startup, and that's it. The value read at startup is passed as
a command line argument to each plugin. The plugins can fallback on working
out the ostype using `uname` if they don't get given one on the command
line.

File Contents

# Content
1 #!/usr/bin/perl -w
2
3 #-----------------------------------------------------------------
4 # i-scream host plugin - uptime & load calculation
5 # $Author: tdb $
6 # $Id: i-scream_uptime.pl,v 1.1 2001/11/19 21:42:11 tdb Exp $
7 #
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 # 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") {
26 # covers: Solaris 7/8
27 # covers: Debian r2.2
28 # covers: FreeBSD 4.X
29 $uptimebin = "/usr/bin/uptime";
30 }
31 else {
32 print "i-scream_uptime.pl Error: Unable to identify system type - \"$ostype\".\n";
33 print "\"uname -s\" does not report one of the following known types;\n";
34 print " SunOS, Linux, FreeBSD\n";
35 exit(1);
36 }
37
38 # Run the following components: -
39 &include_uptime();
40
41 # End the program normally.
42 exit(0);
43
44
45
46
47 # sub to print pairs of data, separated by a single space character.
48 # If the second argument is undefined, then the pair is still printed,
49 # however, the value shall be displayed as the the 'default' value
50 # if the passed value was undefined.
51 sub print_pair($$$) {
52 my($default, $name, $value) = @_;
53
54 if (!defined $value) {
55 $value = $default;
56 }
57
58 # Remove the trailing linefeed if we've not already done so.
59 chomp($value);
60
61 # print the pair of data with a space inbetween.
62 print "$name $value\n";
63 }
64
65 # get system uptime in seconds, and the current load
66 sub include_uptime() {
67
68 # grab the uptime
69 my($uptime) = `$uptimebin`;
70
71 # grab the load
72 &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
73 &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
74 &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
75
76 # work out the days, hours, and minutes
77
78 if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
79 # normal
80 $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
81 $uptime = "$1:$2:$3";
82 }
83 else {
84 if ($uptime =~ /day/) {
85 if ($uptime =~ /hr/) {
86 # 0 minutes
87 $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
88 $uptime = "$1:$2:0";
89 }
90 elsif ($uptime =~ /min/) {
91 # 0 hours
92 $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
93 $uptime = "$1:0:$2";
94 }
95 else {
96 # 0 hours and 0 mins
97 $uptime =~ /up\s+([0-9]+)/;
98 $uptime = "$1:0:0";
99 }
100 }
101 elsif ($uptime =~ /hr/) {
102 # 0 days and 0 minutes
103 $uptime =~ /up\s+([0-9]+)\s+/;
104 $uptime = "0:$1:0";
105 }
106 elsif ($uptime =~ /min/) {
107 # 0 days and 0 hours
108 $uptime =~ /up\s+([0-9]+)\s+/;
109 $uptime = "0:0:$1";
110 }
111 else {
112 # 0 days
113 $uptime =~ /up\s+([0-9]+):([0-9]+)/;
114 $uptime = "0:$1:$2";
115 }
116 }
117
118 # turn into seconds
119 $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
120 $uptime = ($3+($2+($1*24))*60)*60;
121
122 # print the value out
123 &print_pair("unknown", "packet.os.uptime", $uptime);
124
125 }