1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#! @PREFIX@/bin/perl
# $NetBSD: sys2mrtg,v 1.4 2009/01/14 12:20:57 sketch Exp $
if (@ARGV < 1)
{
goto usage;
}
$cmd = @ARGV[0];
if ($cmd eq phys)
{
&vmstat("pages active", "pages free", "bytes per page");
}
if ($cmd eq swap)
{
&vmstat("swap pages in use", "", "bytes per page");
}
if ($cmd eq pages)
{
&vmstat("pagein requests", "pageout requests");
}
if ($cmd eq forks)
{
&vmstat("forks total");
}
if ($cmd eq disks)
{
if (@ARGV < 2)
{
$diskname = "Total"
}
else
{
$diskname = @ARGV[1];
}
$total = 0;
open(CMDOUT, "iostat -xI|");
while (<CMDOUT>)
{
chop $_;
if (m|^(\w+)[\t\s]+[\d\.]+[\t\s]+[\d\.]+[\t\s]+[\d\.]+[\t\s]+([\d\.]+)|)
{
if ($1 eq $diskname)
{
$val = $2 * 1000;
print "$val\n";
print "0\n";
&uptime;
print "$diskname\n";
exit(0);
}
$total = $total + $2 * 1000;
}
}
print "$total\n";
print "0\n";
&end;
}
if ($cmd eq irq)
{
if (@ARGV < 2)
{
$irqname = "Total"
}
else
{
$irqname = @ARGV[1];
}
open(CMDOUT, "vmstat -i|");
while (<CMDOUT>)
{
chop $_;
if (m|^(\w+)[\t\s]+(-?\d+)|)
{
$val = $2;
if ($val < 0) {
$val = 0;
}
if ($1 eq $irqname)
{
print "$val\n";
}
}
}
print "0\n";
if (@ARGV < 2)
{
&end;
}
else
{
&uptime;
print "$irqname\n";
}
}
if ($cmd eq load)
{
open(UP, "uptime|");
while($line = <UP>)
{
chop $line;
if ($line =~ m|^.*up\s+(.+),\s+\d+\suser.*load averages:[\s\t]+([\d\.]+),[\s\t]+[\d\.]+,[\s\t]+[\d\.]+$|)
{
printf("%d\n", $2 * 100);
print "0\n";
print "$1\n";
}
}
close(UP);
open(HOST, "hostname|");
while (<HOST>)
{
s/\..*//;
print $_;
}
close(HOST);
exit(0);
}
&usage();
sub usage
{
print STDERR
"usage: sys2mrtg (phys|swap|pages|irq [name]|forks|disks[name])|load\n";
exit(1);
}
sub vmstat
{
local($f1, $f2,$fmult) = @_;
$mult=1;
$val1=0;
$val2=0;
open(CMDOUT, "vmstat -s|");
while (<CMDOUT>)
{
chop $_;
if (m|(\d+)[\t\s]+(.+)|)
{
$val = $1;
$field = $2;
if ($field eq $fmult)
{
$mult = $val;
}
if ($field eq $f1)
{
$val1 = $val * $mult;
}
if ($field eq $f2)
{
$val2 = $val * $mult;
}
}
}
print "$val1\n";
print "$val2\n";
&end;
}
sub uptime
{
open(UP, "uptime|");
while($line = <UP>)
{
chop $line;
if ($line =~ m|^.*up\s+(.+),\s+\d+\suser.*|)
{
print "$1\n";
}
}
close(UP);
}
sub end
{
&uptime;
open(HOST, "hostname|");
while (<HOST>)
{
s/\..*//;
print $_;
}
exit(0);
}
|