-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.pm
361 lines (305 loc) · 9.19 KB
/
utils.pm
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package utils;
use warnings;
use strict;
use 5.20.1;
use File::stat;
use POSIX;
use Term::ANSIColor;
use Config::IniFiles;
use Digest::MD5 qw(md5_hex);
use Scalar::Util qw(looks_like_number);
use pg_dbi;
use Time::HiRes qw(usleep gettimeofday);
our $configFile;
our $config;
our $configAge;
our $checkDirectory;
our $cacheFile;
our $testmode;
our $widenoverflow;
sub cacheChecks {
my ($UI) = @_;
open my $FH, ">", $cacheFile;
use Data::Dumper;
#say STDERR Dumper($config->{checks});
$Data::Dumper::Deepcopy = 1;
my $cache = {};
foreach my $check ( @{ $UI->{checks} } ) {
$cache->{ $check->{identifier} } = {};
$cache->{ $check->{identifier} }->{metric} = $check->{metric};
$cache->{ $check->{identifier} }->{oldmetric} = $check->{oldmetric};
}
say $FH Dumper($cache);
close $FH;
return 0;
}
sub loadCache {
my $VAR1;
open( my $FH, "<", $cacheFile )
|| ( ErrLog( "could not read cache", "UTILS", "WARN" ) and return );
eval( join( '', <$FH> ) )
|| ( ErrLog( "could not parse cache", "UTILS", "WARN" ) and return );
close $FH;
$config->{cache} = $VAR1;
}
sub widen {
# Textual function #
# Given a lines width, a text and a number if equally distributed elements,
# the function will figure out the space required for each element.
# <------totalWidth*1Character------------->
# | text | n2 | nx | .. | nx |
# text will get widened via a filling character to match the requested width.
my ( $totalWidth, $text, $cnt, $extra, $char ) = @_;
my $textwidth = length($text) + $extra; #for |seperator
my $widthper = floor( $totalWidth / $cnt );
if ( defined $widenoverflow ) { $textwidth -= $widenoverflow; }
my $widthLeft = floor( $widthper / 2 - $textwidth / 2 );
my $widthRight = ceil( $widthper / 2 - $textwidth / 2 );
my $out =
fillwith( $char, $widthLeft ) . $text . fillwith( $char, $widthRight );
if ( defined $widenoverflow ) {
$widenoverflow = $widthLeft + $widthRight;
if ( $widenoverflow > 0 ) { $widenoverflow = 0 }
}
return $out;
}
sub fillwith {
# Simply provide a string of $char*$len
my ( $char, $len ) = @_;
my $out = "";
for ( my $i = 0 ; $i < $len ; $i++ ) {
$out .= $char;
}
return $out;
}
sub storePID {
open my $pidFH, ">", "pid";
print $pidFH $$;
close $pidFH;
}
sub removePID {
unlink "pid";
}
sub getValueOfOptOrDefault {
my ( $opts, $val, $default ) = @_;
if ( ($opts) and ($val) and ( $opts =~ $val ) ) {
my $start = index( $opts, $val ) + length($val);
my $end = index( $opts, " ", $start );
if ( $end < 0 ) { $end = 999 }
return int( substr( $opts, $start, $end - $start ) );
}
return $default;
}
sub redirectSTDERR {
my ($log) = @_;
#say STDERR "Redirecting Log to $log";
open my $log_fh, '>>', $log
or ( ErrLog( "Could not bind new errorstream.", "UTILS", "WARN" )
and return );
*STDERR = $log_fh;
binmode( STDERR, ":utf8" );
}
sub getMD5ofFile {
# hash the content of a file.
# magicnumbers are disabled - not used for now.
my $file = shift;
open( my $FILE, $file );
binmode($FILE);
my $output = Digest::MD5->new->addfile($FILE)->hexdigest;
close($FILE);
return $output;
}
sub reloadConf {
my $configfile = shift;
# a new configuration will be deployed, so if any db-connections exist,
# remove them.
if ( exists $config->{DB} ) { $config->{DB}->{dbh}->disconnect; }
# read the configurationfile.
$configAge = localtime;
open( my $FH, "<", $configFile )
|| ( ErrLog( "could not read config", "UTILS", "FATAL" ) and exit 1 );
eval( join( '', <$FH> ) )
|| ( ErrLog( "could not parse config", "UTILS", "FATAL" ) and exit 1 );
close $FH;
# did we just load a config or a cachefile?
# cachefiles provide $config1
# If it's not a cachefile, check-configurations need to be provided.
# They reside within configs in the checks/ folder.
# Load all of 'em.
opendir my $checkdir,
$checkDirectory || die "Can't open check-directory: $!\n";
while ( my $f = readdir $checkdir ) {
my $checks;
if ( $f =~ /^\.+/ ) { next; }
$checks = Config::IniFiles->new( -file => "$checkDirectory/$f" )
or ( ErrLog( "cantLoad $f", "UTILS", "WARN" ) and next );
foreach my $chk ( $checks->Sections() ) {
$config->{checks}->{$chk} = {};
foreach my $param ( $checks->Parameters($chk) ) {
$config->{checks}->{$chk}->{$param} =
eval( $checks->val( $chk, $param ) )
or (
ErrLog(
"parsing $param for $chk failed: $@ ", "UTILS",
"WARN"
)
and next
);
}
}
}
closedir $checkdir;
$config->{DB} = pg_dbi::new( config => $config );
# for each check, load the required plugin.
# abit messy but require shouldnt reload multiple times - we're fine for now.
foreach my $key ( keys %{ $config->{checks} } ) {
require "plugins/" . $config->{checks}->{$key}->{plugin} . ".pm"
or ErrLog( "could not load $key", "UTILS", "WARN" );
}
# same for UIs.
foreach my $key ( keys %{ $config->{UI} } ) {
require "UI/" . $config->{UI}->{$key}->{template} . ".pm"
or print "could not load $key";
bless( $config->{UI}->{$key}, $config->{UI}->{$key}->{template} );
}
if ( exists $config->{log} ) {
redirectSTDERR( $config->{log} );
}
return $config;
}
sub ensureCheck {
my ($check) = @_;
if ( ref $check eq "HASH" ) {
$check = utils::checkfactory($check);
}
}
sub checkfactory {
my ($target) = @_;
my $targetcheck = $target->{check};
my $targetclass = $config->{checks}->{$targetcheck}->{plugin};
my $output = $targetclass->new($target);
return $output;
}
sub checkAndReloadConfig {
open my $FH, "<", $configFile
or return 1; #It is possible that opening the config fails.
if ( ( not exists $config->{age} )
or ( stat($FH)->mtime != $config->{age} ) )
{
$config = reloadConf($configFile);
$config->{age} = stat($FH)->mtime;
close $FH;
return 1;
}
close $FH;
return 0;
# }
}
sub stampbegin {
my ($obj) = @_;
$obj->{initstamp} = gettimeofday();
return;
}
sub stampend {
my ($obj) = @_;
$obj->{endstamp} = gettimeofday();
}
sub formatter {
my ( $val, $unit, $obj ) = @_;
if ( not defined $unit ) { $unit = "" }
if ( ( $config->{humanreadable} eq 0 )
or ( $obj->{base}->{isHumanreadable} ) )
{
if ( $unit eq "N" ) { $unit = ''; }
return $val . $unit;
}
if ( $unit eq "N" ) {
$unit = "";
if ( ( abs($val) >= 999 ) ) {
$unit = 'K';
$val /= 1000;
}
if ( ( abs($val) >= 999 ) ) {
$unit = 'M';
$val /= 1000;
}
}
unless ( defined $unit ) { $unit = "" }
if ( $unit eq "B" ) {
if ( abs($val) >= 999 ) {
$val /= 1024;
$unit = "KB";
}
}
if ( $unit eq "KB" ) {
if ( abs($val) >= 999 ) {
$val /= 1024;
$unit = "MB";
}
}
if ( $unit eq "MB" ) {
if ( abs($val) >= 1000 ) {
$val /= 1024;
$unit = "GB";
}
}
return sprintf( "%.0f", $val ) . $unit;
}
sub checkTiming {
my ($check) = @_;
if (
( exists $config->{timing} )
and ( ( $check->{endstamp} - $check->{initstamp} ) * 1000 >=
$config->{timing} )
)
{
utils::ErrLog(
ceil( 1000 * ( $check->{endstamp} - $check->{initstamp} ) ) . "ms",
$check->{identifier} . ' via UTILS',
"INFO"
);
}
}
sub colorswitch {
my ($clr) = @_;
if ( $config->{color} eq 1 ) {
return color($clr);
}
return "";
}
sub ErrLog {
my ( $msg, $sender, $type ) = @_;
my $logtypes = [ "FATAL", "WARN", "INFO", "debug" ];
foreach ( @{$logtypes} ) {
if ( $type eq $_ ) { last; }
if ( ( exists $config->{loglevel} ) and ( $config->{loglevel} eq $_ ) )
{
return;
}
}
say STDERR "[" . localtime . "] " . $type . " " . $sender . ":" . $msg;
STDERR->flush();
}
{
my $loopStart;
sub loopSleep {
my ($updatetime) = @_;
my $now = gettimeofday;
if ( not defined $loopStart ) { $loopStart = $now }
my $timetosleep = ( $updatetime - ( $now - $loopStart ) * 1000000 );
if ( $utils::config->{sync} ) {
my $sleepfix = ( $now * 1000 ) % ( $updatetime / 1000 );
if ( $sleepfix * 1000 > $updatetime / 2.0 ) {
$sleepfix = -( $updatetime / 1000 - $sleepfix );
}
$timetosleep = $timetosleep - 300 * $sleepfix;
}
if ( $timetosleep < 0 ) {
utils::ErrLog( "Queries take too long for loop!", "WALL", "WARN" );
$timetosleep = 0;
}
usleep $timetosleep;
$loopStart = gettimeofday;
}
}
1;