-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-procfs.pl
executable file
·335 lines (322 loc) · 13.4 KB
/
check-procfs.pl
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/perl
############################################################################
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
## © 2013 Jesse Wiley <jesse.wiley.viacom.com ##
## ##
############################################################################
use strict;
use Time::Local;
use POSIX;
use Getopt::Long;
my $stats_file="/tmp/cpu-stats";
my $localtime=time;
my %old_stats;
my %new_stats;
my %stored;
my $cacti;
my $switch;
my $pipe;
my $metric;
my $threshold;
my $debug;
my $help;
my $first_run;
my $empty;
my $metricName;
GetOptions ('cacti' => \$cacti, 'pipe' => \$pipe, 'switch' => \$switch, 'metric=s' => \$metric, 'threshold=s' => \$threshold, 'debug' => \$debug, 'help' => \$help);
if ($help){
print "Usage: check_stolen.pl\n";
print "\t-d = Show Debug Output\n";
print "\t-c = Print Output for Cacti CPU Usage Graphing\n";
print "\t-s = Print Output for Cacti CPU Context Switch Graphing\n";
print "\t-p = Print Output for open Sockets,Files & Pipes\n";
print "\t-m = Cpu Metric to check\n";
print "\t\tMetrics:\n";
print "\t\t\tuser\n";
print "\t\t\tnice\n";
print "\t\t\tsystem\n";
print "\t\t\tidle\n";
print "\t\t\tiowait\n";
print "\t\t\tirq\n";
print "\t\t\tsoft_irq\n";
print "\t\t\tstolen\n";
print "\t\t\tcontext\n";
print "\t\t\tintr\n";
print "\t-t = Threshold to check CPU Metric against\n";
print "\t-h = This help message\n\n";
exit 0;
}
if ($pipe){
print "Doing work for Pipe/Socket/Files Data\n" if ($debug);
&do_pipe;
}
if ($cacti || $switch){
print "Found Cacti, Undefining metric $metric\n" if ($debug);
undef $metric;
undef $pipe;
}
if ($metric){
undef $cacti;
undef $switch;
undef $pipe;
$metricName=ucfirst $metric;
}
if (!$metric){
if (!$cacti && !$switch && !$pipe){
print "cacti:$cacti\n";
print "switch:$switch\n";
print "CRITICAL: No Pipe, Metric or Cacti option supplied\n";
exit 2;
}
}
if ($metric){
chomp $metric;
if (
$metric eq "user" ||
$metric eq "nice" ||
$metric eq "system" ||
$metric eq "idle" ||
$metric eq "iowait" ||
$metric eq "irq" ||
$metric eq "soft_irq" ||
$metric eq "context" ||
$metric eq "intr" ||
$metric eq "stolen"
){
print "Using CPU Metric $metric\n" if ($debug);
if (!$threshold){
print "Threshold Not Found-Defaulting to 0\n" if ($debug);
$threshold=0;
}elsif($threshold=~/[a-zA-Z]/){
print "THREShOLD is a char, not a num: $threshold\n" if ($debug);
$threshold=0;
}else{
print "Using arg threshold:$threshold\n" if ($debug);
$threshold=$threshold;
}
}else{
print "CRITICAL: Incorrect CPU Metric supplied:$metric:";
print "\n" if ($debug);
exit 2;
}
}
if ($debug){
print "Options:\n";
print "\tcacti:\t$cacti\n";
print "\tswitch:\t$switch\n";
print "\tmetric:\t$metric\n";
print "\tthresh:\t$threshold\n";
print "\tdebug:\t$debug\n";
}
if (!-f $stats_file){
system("touch $stats_file");
$first_run=1;
}else{
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($stats_file);
if ($size < 76){
$empty=1;
}else{
open(FILE, $stats_file);
my @lines=<FILE>;
close FILE;
foreach my $item(@lines){
chomp $item;
my @split=split(":", $item);
$old_stats{$split[0]}=$split[1];
}
}
}
open(OUT, ">$stats_file");
close OUT;
open (OUT, ">>$stats_file");
my $line=`head -1 /proc/stat`;
chomp $line;
print "Cpu Line: $line\n" if ($debug);
my $ctxt_line=`grep "ctxt" /proc/stat`;
chomp $ctxt_line;
print "Context Switch Line: $ctxt_line\n" if ($debug);
my ($ctxt_name,$ctxt) = split(" ", $ctxt_line);
my $intr_line=`grep "intr" /proc/stat`;
chomp $intr_line;
my ($intr_name,$intr) = split(" ", $intr_line);
#cpu 79670799 0 138398240 16873901221 26829901 1729092 27566872 62622372
my ($cpu, $user, $nice, $system, $idle, $iowait, $irq, $soft_irq, $stolen) = split(" ", $line);
my $total=$user+$nice+$system+$idle;
$new_stats{'user'}=$user; print OUT "user:$user\n";
$new_stats{'nice'}=$nice; print OUT "nice:$nice\n";
$new_stats{'system'}=$system; print OUT "system:$system\n";
$new_stats{'idle'}=$idle; print OUT "idle:$idle\n";
$new_stats{'iowait'}=$iowait; print OUT "iowait:$iowait\n";
$new_stats{'irq'}=$irq; print OUT "irq:$irq\n";
$new_stats{'soft_irq'}=$soft_irq; print OUT "soft_irq:$soft_irq\n";
$new_stats{'context'}=$ctxt; print OUT "context:$ctxt\n";
$new_stats{'intr'}=$intr; print OUT "intr:$intr\n";
$new_stats{'stolen'}=$stolen; print OUT "stolen:$stolen\n";
$new_stats{'total'}=$total; print OUT "total:$total\n";
$new_stats{'localtime'}=$localtime; print OUT "localtime:$localtime\n";
if ($first_run == 1 && !$cacti){
print "CRITICAL: Created $stats_file\n";
exit(2);
}
if ($empty == 1 && !$cacti){
print "CRITICAL: $stats_file is empty\n";
exit(2);
}
print "*********************************\n" if ($debug);
print "CURRENT STOLEN:\t\t$stolen\n" if ($debug);
print "CURRENT USER:\t\t$user\n" if ($debug);
print "CURRENT SYSTEM:\t\t$system\n" if ($debug);
print "CURRENT NICE:\t\t$nice\n" if ($debug);
print "CURRENT IDLE:\t\t$idle\n" if ($debug);
print "CURRENT IOWAIT:\t\t$iowait\n" if ($debug);
print "CURRENT IRQ:\t\t$irq\n" if ($debug);
print "CURRENT SOFT IRQ:\t$soft_irq\n" if ($debug);
print "CURRENT CTXT SWITCH:\t$ctxt\n" if ($debug);
print "CURRENT INTERRUPTS:\t$intr\n" if ($debug);
print "CURRENT TOTAL:\t\t$total\n" if ($debug);
print "CURRENT TIME:\t\t$localtime\n" if ($debug);
print "\n" if ($debug);
print "PREVIOUS STOLEN:\t$old_stats{'stolen'}\n" if ($debug);
print "PREVIOUS USER:\t\t$old_stats{'user'}\n" if ($debug);
print "PREVIOUS SYSTEM:\t$old_stats{'system'}\n" if ($debug);
print "PREVIOUS NICE:\t\t$old_stats{'nice'}\n" if ($debug);
print "PREVIOUS IDLE:\t\t$old_stats{'idle'}\n" if ($debug);
print "PREVIOUS IOWAIT:\t$old_stats{'iowait'}\n" if ($debug);
print "PREVIOUS IRQ:\t\t$old_stats{'irq'}\n" if ($debug);
print "PREVIOUS SOFT IRQ:\t$old_stats{'soft_irq'}\n" if ($debug);
print "PREVIOUS CTXT SWITCH:\t$old_stats{'context'}\n" if ($debug);
print "PREVIOUS INTERRUPTS:\t$old_stats{'intr'}\n" if ($debug);
print "PREVIOUS TOTAL:\t\t$old_stats{'total'}\n" if ($debug);
print "PREVIOUS TIME:\t\t$old_stats{'localtime'}\n" if ($debug);
print "*********************************\n\n" if ($debug);
$stored{'localtime'}=$new_stats{'localtime'}-$old_stats{'localtime'};
$stored{'total'}=$new_stats{'total'}-$old_stats{'total'};
$stored{'stolen'}=$new_stats{'stolen'}-$old_stats{'stolen'};
$stored{'user'}=$new_stats{'user'}-$old_stats{'user'};
$stored{'system'}=$new_stats{'system'}-$old_stats{'system'};
$stored{'idle'}=$new_stats{'idle'}-$old_stats{'idle'};
$stored{'nice'}=$new_stats{'nice'}-$old_stats{'nice'};
$stored{'context'}=$new_stats{'context'}-$old_stats{'context'};
$stored{'intr'}=$new_stats{'intr'}-$old_stats{'intr'};
my $percent=substr(($stored{$metric}/$stored{'total'})*100,0,4);
my $percent_user=ceil(($stored{'user'}/$stored{'total'})*100);
my $percent_nice=ceil(($stored{'nice'}/$stored{'total'})*100);
my $percent_system=ceil(($stored{'system'}/$stored{'total'})*100);
my $percent_idle=ceil(($stored{'idle'}/$stored{'total'})*100);
my $percent_iowait=ceil(($stored{'iowait'}/$stored{'total'})*100);
my $percent_irq=ceil(($stored{'irq'}/$stored{'total'})*100);
my $percent_soft_irq=ceil(($stored{'soft_irq'}/$stored{'total'})*100);
my $percent_stolen=ceil(($stored{'stolen'}/$stored{'total'})*100);
if ($cacti && !$switch){
print "Printing CPU Usage data for Cacti\n" if ($debug);
print "user:$percent_user nice:$percent_nice system:$percent_system idle:$percent_idle iowait:$percent_iowait irq:$percent_irq soft_irq:$percent_soft_irq stolen:$percent_stolen";
print "\n" if ($debug);
exit 0;
}
if ($switch && !$cacti){
print "Printing CPU Context Switch/Interrupt data for Cacti\n" if ($debug);
print "context:$stored{'context'} intr:$stored{'intr'}";
print "\n" if ($debug);
exit 0;
}
if ($metric eq "context"){
print "Nagios Context Switch output:\n" if ($debug);
if ($stored{$metric} eq ""){
print "WARNING:No Context Switches Recorded";
exit 1;
}else{
if ($stored{$metric} <= $threshold){
print "OK: Context Switches=$stored{$metric} in $stored{'localtime'}/s";
exit 0;
}else{
print "CRITICAL: Context Switches=$stored{$metric} in $stored{'localtime'}/s";
exit 2;
}
}
}
if ($metric eq "intr"){
print "Nagios Interrupts output:\n" if ($debug);
if ($stored{$metric} eq ""){
print "WARNING:No Interrupts Recorded";
exit 1;
}else{
if ($stored{$metric} <= $threshold){
print "OK: Interrupts Switches=$stored{$metric} in $stored{'localtime'}/s";
exit 0;
}else{
print "CRITICAL: Interrupts Switches=$stored{$metric} in $stored{'localtime'}/s";
exit 2;
}
}
}
print "Time since last run: $stored{'localtime'} seconds\n" if ($debug);
print "Aggregate Total: $stored{'total'}\n" if ($debug);
print "Aggregate $metric: $stored{$metric}\n" if ($debug);
print "Aggregate Percentage: $percent\%\n" if ($debug);
if ($stored{$metric} eq ""){
print "CRITICAL: CPU $metricName Data is unreadable";
print "$metric:$stored{$metric}\n" if ($debug);
exit 2;
}elsif ($stored{$metric} == 0 && 0 <= $threshold){
print "OK: $metricName CPU: $percent in $stored{'localtime'}/s";
exit 0;
}elsif(!$stored{'localtime'}){
print "CRITICAL: Time Data is unreadable";
exit 2;
}else{
if ($percent > $threshold && $stored{'localtime'} > 0){
print "$percent > $threshold\n" if ($debug);
print "CRITICAL: $metricName CPU Time - $percent\% in $stored{'localtime'}/s";
exit 2;
}elsif($percent > $threshold && $stored{'localtime'} == 0){
print "$percent > $threshold\n" if ($debug);
print "WARNING: Time Elapsed is 0, Wait longer.$metricName: $percent\%";
exit 1;
}elsif($percent == $threshold && $stored{'localtime'} == 0){
print "$percent == $threshold\n" if ($debug);
print "WARNING: Time Elapsed is 0, Wait longer.$metricName: $percent\%";
exit 1;
}elsif($percent <= $threshold && $stored{'localtime'} > 0){
print "$percent <= $threshold\n" if ($debug);
print "OK: $metricName CPU: $percent\% in $stored{'localtime'}/s";
exit 0;
}else{
print "UNKNOWN: Data is unreadable";
exit 3;
}
}
sub do_pipe{
print "Inserted into do_pipe\n" if ($debug);
#sleep 5;
my $files=`cat /proc/sys/fs/file-nr|cut -f1`;
chomp $files;
print "Open Files: $files\n" if ($debug);
my $pipes=`lsof -Ft 2>&1 | grep FIFO | wc -l`;
chomp $pipes;
print "Open Pipes: $pipes\n" if ($debug);
my $tcp=`sudo /bin/netstat -anp | grep tcp | wc -l`;
chomp $tcp;
print "Open TCP Sockets: $tcp\n" if ($debug);
my $udp=`sudo /bin/netstat -anp | grep udp | wc -l`;
chomp $udp;
print "Open UDP Sockets: $udp\n" if ($debug);
my $unix=`sudo /bin/netstat -anp | grep unix | wc -l`;
chomp $unix;
print "Open Unix Sockets: $unix\n" if ($debug);
#echo -n "files:$files pipes:$pipes tcp:$tcp udp:$udp unix:$unix" > /tmp/sockets_pipes_files.tmp
print "files:$files pipes:$pipes tcp:$tcp udp:$udp unix:$unix";
exit 0;
}