1 |
#!/usr/bin/perl -w |
2 |
# |
3 |
# This script will create Default files for two vmstat veriants |
4 |
# |
5 |
# 2003-10-27 Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
# |
7 |
# Due to cricket's eagerness to execute first data sources, your data |
8 |
# *WILL BE* delayed by 5 minutes (or one cricket interval) on your |
9 |
# graphs. Reason is that cricket first tries to read file (from last |
10 |
# run) and then executes vmstat which will produce new file. |
11 |
|
12 |
use strict; |
13 |
use Data::Dumper; |
14 |
|
15 |
# variant (-s for vmstat -s) |
16 |
my $is_s = shift @ARGV; |
17 |
|
18 |
my $target_type = "vmstat"; |
19 |
$target_type = "vmstat-stat" if ($is_s); |
20 |
|
21 |
my $target_def_fmt = qq( |
22 |
Target --default-- |
23 |
directory-desc = "Report virtual memory statistics" |
24 |
short-desc = "Virtual memory" |
25 |
|
26 |
target-type = %s |
27 |
|
28 |
# by default don't use ssh |
29 |
ssh="" |
30 |
|
31 |
# comment following line if you don't want alerts for filespace |
32 |
# to be created in /var/log/cricket/alters |
33 |
# change n to number for low mark and 90 to high mark |
34 |
#monitor-thresholds = "df : value : n : 90 : FILE : /var/log/cricket/alerts" |
35 |
); |
36 |
|
37 |
my $dataSource_fmt = qq( |
38 |
dataSource %s |
39 |
rrd-ds-type = %s |
40 |
rrd-heartbeat = 1800 |
41 |
ds-source = "%s:%s:%s" |
42 |
# rrd-min = 0 |
43 |
# rrd-max = undef |
44 |
precision = integer |
45 |
); # name, type=GAUGE|DERIVE, 0-x, script |
46 |
|
47 |
my $graph_fmt = qq( |
48 |
graph %s |
49 |
draw-as = %s |
50 |
legend = "%s" |
51 |
units = "%s" |
52 |
); # name, AREA|LINE3, legend, y-axis, units |
53 |
|
54 |
|
55 |
my $targetType_fmt = qq( |
56 |
targetType %s |
57 |
ds = "%s" |
58 |
view = "%s" |
59 |
# y-min = 0 |
60 |
# y-max = 100 |
61 |
|
62 |
graph --default-- |
63 |
height = 278 |
64 |
height-hint = 278 |
65 |
); # name, ds (separated by spaces), view (Name: ds ds[,..]) |
66 |
|
67 |
my $target_fmt = qq( |
68 |
target %s |
69 |
ssh = %s |
70 |
); |
71 |
|
72 |
# definition for VM mode (copy/paster from vmstat man page :-) |
73 |
|
74 |
my $ds_type = { |
75 |
# vmstat |
76 |
'si' => 'ABSOLUTE', |
77 |
'so' => 'ABSOLUTE', |
78 |
'bi' => 'ABSOLUTE', |
79 |
'bo' => 'ABSOLUTE', |
80 |
# vmstat -s |
81 |
'nnct' => 'COUNTER', |
82 |
'nct' => 'COUNTER', |
83 |
'sct' => 'COUNTER', |
84 |
'ict' => 'COUNTER', |
85 |
'ioct' => 'COUNTER', |
86 |
'ppi' => 'COUNTER', |
87 |
'ppo' => 'COUNTER', |
88 |
'psi' => 'COUNTER', |
89 |
'pso' => 'COUNTER', |
90 |
'int' => 'COUNTER', |
91 |
'ccs' => 'COUNTER', |
92 |
'bt' => 'COUNTER', |
93 |
'fork' => 'COUNTER', |
94 |
}; |
95 |
|
96 |
my $definition = qq( |
97 |
Procs |
98 |
r: The number of processes waiting for run time. |
99 |
b: The number of processes in uninterruptible sleep. |
100 |
|
101 |
Memory |
102 |
swpd: the amount of virtual memory used. |
103 |
free: the amount of idle memory. |
104 |
buff: the amount of memory used as buffers. |
105 |
cache: the amount of memory used as cache. |
106 |
# inact: the amount of inactive memory. (-a option) |
107 |
# active: the amount of active memory. (-a option) |
108 |
|
109 |
Swap |
110 |
si: Amount of memory swapped in from disk [amount/s]. |
111 |
so: Amount of memory swapped to disk [amount/s]. |
112 |
|
113 |
IO |
114 |
bi: Blocks received from a block device [blocks/s]. |
115 |
bo: Blocks sent to a block device [blocks/s]. |
116 |
|
117 |
System |
118 |
in: The number of interrupts per second, including the clock. |
119 |
cs: The number of context switches per second. |
120 |
|
121 |
CPU |
122 |
us: Time spent running non-kernel code. (user time, including nice time) |
123 |
sy: Time spent running kernel code. (system time) |
124 |
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time. |
125 |
wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero. |
126 |
); |
127 |
|
128 |
# definition for vmstat -s created using vmstat -s -S K | cut -c15- |
129 |
# with editing for units (encosed in []) |
130 |
$definition = qq( |
131 |
Memory |
132 |
tm: [kB] total memory |
133 |
um: [kB] used memory |
134 |
am: [kB] active memory |
135 |
im: [kB] inactive memory |
136 |
fm: [kB] free memory |
137 |
bm: [kB] buffer memory |
138 |
Swap |
139 |
sc: [kB] swap cache |
140 |
ts: [kB] total swap |
141 |
us: [kB] used swap |
142 |
fs: [kB] free swap |
143 |
Ticks |
144 |
nnct: non-nice user cpu ticks [ticks] |
145 |
nct: nice user cpu ticks [ticks] |
146 |
sct: system cpu ticks [ticks] |
147 |
ict: idle cpu ticks [ticks] |
148 |
ioct: IO-wait cpu ticks [ticks] |
149 |
Pages |
150 |
ppi: pages paged in [pages] |
151 |
ppo: pages paged out [pages] |
152 |
psi: pages swapped in [pages] |
153 |
pso: pages swapped out [pages] |
154 |
Interrupts |
155 |
int: interrupts |
156 |
Context_switches |
157 |
ccs: CPU context switches |
158 |
Boot_time |
159 |
bt: boot time |
160 |
Forks |
161 |
fork: forks |
162 |
) if ($is_s); |
163 |
|
164 |
my @ds; |
165 |
my @views; |
166 |
my $view_ds; |
167 |
my $ds_unit; |
168 |
my $desc; |
169 |
|
170 |
# start parsing at which element |
171 |
my $ds_max = 0; |
172 |
my $curr_view = "Default"; |
173 |
|
174 |
foreach (split(/[\n\r]+/,$definition)) { |
175 |
chomp; |
176 |
next if (/^$/ || /^#/); |
177 |
if (/^\s*(\w+)\s*$/) { |
178 |
# view definition |
179 |
$curr_view = $1; |
180 |
push @views, $curr_view; |
181 |
} elsif (/^\s*(\w+):\s+(.+)$/) { |
182 |
# source name: description |
183 |
my ($ds,$description) = ($1,$2); |
184 |
$ds_max++; |
185 |
push @ds,$ds; |
186 |
push @{$view_ds->{$curr_view}},$ds; |
187 |
if ($description =~ s/\s*\[([^\]]+)\]\s*//) { |
188 |
$ds_unit->{$ds} = $1; |
189 |
} |
190 |
$desc->{$ds} = $description; |
191 |
} else { |
192 |
print STDERR "unparsable line: $_\n"; |
193 |
} |
194 |
} |
195 |
|
196 |
# don't use value 0 it's empty! |
197 |
my $vmstat = qq(\%ssh\% vmstat 1 2 | tail -1 | sed 's/ */\\\\n/'g); |
198 |
my $ds_start = 1; |
199 |
|
200 |
if ($is_s) { |
201 |
$vmstat = qq(\%ssh\% vmstat -s -S K); |
202 |
$ds_start = 0; |
203 |
} |
204 |
|
205 |
|
206 |
# dump Target |
207 |
printf($target_def_fmt,$target_type); |
208 |
|
209 |
|
210 |
# dump targetType |
211 |
my $view_str; |
212 |
foreach (keys %{$view_ds}) { |
213 |
$view_str .= "$_: ".join(" ",@{$view_ds->{$_}}).", "; |
214 |
} |
215 |
$view_str =~ s/, $//; |
216 |
printf($targetType_fmt, $target_type, join(", ",@ds), $view_str); |
217 |
|
218 |
|
219 |
# dump dataSource |
220 |
my $i=$ds_start; # start ds on line |
221 |
foreach my $ds (@ds) { |
222 |
my $cmd = $vmstat; |
223 |
my $scheme = "exec"; |
224 |
|
225 |
#if ($i == $ds_start) { |
226 |
# # first ds dumps vmstat and cache in in tmp file |
227 |
# $cmd = $vmstat; |
228 |
#} elsif ($i == $ds_max) { |
229 |
# # last row should also erase cache |
230 |
# $cmd = "cat $cache && rm $cache"; |
231 |
#} else { |
232 |
# # while all other rows just dump cache file |
233 |
# $scheme = "file"; |
234 |
# $cmd = $cache; |
235 |
# #$cmd = "cat $cache"; |
236 |
#} |
237 |
|
238 |
printf($dataSource_fmt, $ds,$ds_type->{$ds} || 'GAUGE',$scheme,$i,$cmd); |
239 |
|
240 |
$i++; |
241 |
} |
242 |
|
243 |
# dump graph(s) |
244 |
foreach my $ds (@ds) { |
245 |
printf($graph_fmt, $ds, 'LINE3', $desc->{$ds}, $ds_unit->{$ds} || ""); |
246 |
} |
247 |
|