1 |
tdb |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
#----------------------------------------------------------------- |
4 |
|
|
# i-scream host plugin - uptime & load calculation |
5 |
|
|
# $Author: tdb1 $ |
6 |
|
|
# $Id: statgrab.pl,v 1.46 2001/11/14 15:18:25 tdb1 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 |
|
|
# Have to hope this will work really. |
19 |
|
|
my($ostype) = `uname -s`; chomp($ostype); |
20 |
|
|
|
21 |
|
|
# Decide which paths we should use. |
22 |
|
|
my($uptimebin); |
23 |
|
|
if ($ostype eq "SunOS" || $ostype eq "Linux" || $ostype eq "FreeBSD") { |
24 |
|
|
# covers: Solaris 7/8 |
25 |
|
|
# covers: Debian r2.2 |
26 |
|
|
# covers: FreeBSD 4.X |
27 |
|
|
$uptimebin = "/usr/bin/uptime"; |
28 |
|
|
} |
29 |
|
|
else { |
30 |
|
|
print "i-scream_uptime.pl Error: Unable to identify system type - \"$ostype\".\n"; |
31 |
|
|
print "\"uname -s\" does not report one of the following known types;\n"; |
32 |
|
|
print " SunOS, Linux, FreeBSD\n"; |
33 |
|
|
exit(1); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
# Run the following components: - |
37 |
|
|
&print_ident(); |
38 |
|
|
&include_uptime(); |
39 |
|
|
|
40 |
|
|
# End the program normally. |
41 |
|
|
exit(0); |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
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: 1.46 $'; |
50 |
|
|
print "\n"; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
# sub to print pairs of data, separated by a single space character. |
54 |
|
|
# If the second argument is undefined, then the pair is still printed, |
55 |
|
|
# however, the value shall be displayed as the the 'default' value |
56 |
|
|
# if the passed value was undefined. |
57 |
|
|
sub print_pair($$$) { |
58 |
|
|
my($default, $name, $value) = @_; |
59 |
|
|
|
60 |
|
|
if (!defined $value) { |
61 |
|
|
$value = $default; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
# Remove the trailing linefeed if we've not already done so. |
65 |
|
|
chomp($value); |
66 |
|
|
|
67 |
|
|
# print the pair of data with a space inbetween. |
68 |
|
|
print "$name $value\n"; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
# get system uptime in seconds, and the current load |
72 |
|
|
sub include_uptime() { |
73 |
|
|
|
74 |
|
|
# grab the uptime |
75 |
|
|
my($uptime) = `$uptimebin`; |
76 |
|
|
|
77 |
|
|
# grab the load |
78 |
|
|
&print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/); |
79 |
|
|
&print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/); |
80 |
|
|
&print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/); |
81 |
|
|
|
82 |
|
|
# 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"; |
88 |
|
|
} |
89 |
|
|
else { |
90 |
|
|
if ($uptime =~ /day/) { |
91 |
|
|
if ($uptime =~ /hr/) { |
92 |
|
|
# 0 minutes |
93 |
|
|
$uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; |
94 |
|
|
$uptime = "$1:$2:0"; |
95 |
|
|
} |
96 |
|
|
elsif ($uptime =~ /min/) { |
97 |
|
|
# 0 hours |
98 |
|
|
$uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; |
99 |
|
|
$uptime = "$1:0:$2"; |
100 |
|
|
} |
101 |
|
|
else { |
102 |
|
|
# 0 hours and 0 mins |
103 |
|
|
$uptime =~ /up\s+([0-9]+)/; |
104 |
|
|
$uptime = "$1:0:0"; |
105 |
|
|
} |
106 |
|
|
} |
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; |
127 |
|
|
|
128 |
|
|
# print the value out |
129 |
|
|
&print_pair("unknown", "packet.os.uptime", $uptime); |
130 |
|
|
|
131 |
|
|
} |