-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_YAPT.pl
executable file
·196 lines (175 loc) · 6.7 KB
/
pg_YAPT.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
#! /usr/bin/env perl
use strict;
use warnings;
use 5.20.1;
# Set includepath to the local position. This Program will rely heavily on
# local files.
use File::Basename;
# Utils provide basic tools and utils. e.G Config-Handle.
use lib ( dirname $0); #wherever pg_YAPT is, thats where the includes are!
use utils;
$utils::checkDirectory = ( dirname $0) . "/checks"; #dirty for now
# Option Parser
use Getopt::Long;
use Getopt::Long::Descriptive;
# On Sighub, reload config and continue with thatever you did.
sub main {
my $version = "0.0.5";
# Setup the argument parser.
my ( $opt, $usage ) = describe_options(
"Usage: pg_YAPT [opts]",
[],
[ 'ui|u=s', "override the UI-choice from config. ", {} ],
[ 'list|l', "list all checks and configured UIs", {} ],
[ 'uiopts|o=s', "UI-specific options to push down", {} ],
[ 'addcheck|a=s', "add these checks", {} ],
[ 'dbopts|d=s', "use this connectionstring", {} ],
[],
[
'deletecache|D',
"delete cache. necessary if configs changed.",
{ default => 0 }
],
[
'reattachable|r',
"reattach to existing cache. Speeds up diff-based checks",
{ default => 0 }
],
[],
[
'config|C=s',
"config to use",
{ default => ( dirname $0) . "/config.pm" }
],
[
'cache=s',
"cacheFile to use",
{ default => ( dirname $0) . "/.cache.pm" }
],
[],
[ 'timing|t=i', "print checktimes longer X to stderr", {} ],
[ 'humanreadable|h', "print numbers as humanreadable", {} ],
[ 'color|c', "force colors on/off", {} ],
[ 'test|T', "do not connect to the database", {} ],
[ 'sync|S', "try to start on full seconds", { default => 0 } ],
[ 'verbose|v', "print additional info. works good with list", {} ],
[ 'veryverbose|V', "like verbose.. but worse (todo)", {} ],
[ 'help|H', "print usage message and exit" ],
);
print( $usage->text ), exit if $opt->help;
say "pg_YAPT V$version" if $opt->{verbose};
# delete the cachefile if asked to
# doesnt rely on the config, so we can do it very early on
if ( $opt->{deletecache} ) {
unlink $opt->{cache};
}
# now handle the Config.
# reset the utils::config, set the targetfiles properly and force a reload.
$utils::config = undef;
$utils::configFile = $opt->{config};
utils::checkAndReloadConfig();
# if we're asked to be reattachable reset the targetfile to cache and load again.
# because the contents may be blessed, we need to load the defaultconfig first to make
# sure all includes allready exist.
#
# once done, reset the targetfile again to provide proper reload-functionality on sighup.
if ( $opt->{reattachable} ) {
$utils::cacheFile = $opt->{cache};
utils::loadCache();
$utils::config->{Reattachable} = 1;
}
if ( $opt->{test} ) { $utils::config->{tests} = 1; }
if ( exists $opt->{dbopts} ) {
$utils::config->{database}->{connection} = $opt->{dbopts};
}
# config is now loaded. check if there's an override for the UI.
# If not, reset the $opt->{ui} value with the default.
if ( $opt->{list} ) {
say "Existing checks:";
foreach ( sort keys %{ $utils::config->{checks} } ) {
my $doc = "-/-";
my $out = $_;
if ( exists $utils::config->{checks}->{$_}->{doc} ) {
$doc = $utils::config->{checks}->{$_}->{doc};
}
if ( $opt->{verbose} ) {
$out = "["
. $utils::config->{checks}->{$_}->{plugin} . "]"
. utils::fillwith( " ",
11 - length( $utils::config->{checks}->{$_}->{plugin} ) )
. $out
. utils::fillwith( " ", 9 - length($out) ) . ": "
. $doc;
}
say "" . $out;
}
say "";
}
unless ( exists $opt->{ui} ) { $opt->{ui} = $utils::config->{defaultui}; }
if ( exists $opt->{timing} ) { $utils::config->{timing} = $opt->{timing}; }
if ( exists $opt->{sync} ) { $utils::config->{sync} = $opt->{sync}; }
if ( exists $opt->{color} )
{ $utils::config->{color} = $opt->{color}; }
elsif ( not exists $utils::config->{color} )
{ $utils::config->{color} = 0 }
if ( exists $opt->{humanreadable} )
{$utils::config->{humanreadable} = $opt->{humanreadable}; ;}
elsif (not exists $utils::config->{humanreadable} )
{$utils::config->{humanreadable} = 0;}
# Now check if the requested UI actually exists.
if ( !exists $utils::config->{UI}->{ $opt->{ui} } ) {
utils::ErrLog( "Unknown UI:" . $opt->{ui}, "main", "FATAL" );
}
# If the UI dosnt exist, OR if we're asked to list all possible UIs
# print all UIs.
if ( ( !exists $utils::config->{UI}->{ $opt->{ui} } )
or ( $opt->{list} ) )
{
say "Existing UIs:";
foreach ( sort keys %{ $utils::config->{UI} } ) {
my $line = $_;
if ( $opt->{verbose} and ( $utils::config->{defaultui} eq $_ ) ) {
$line = "*" . $line;
}
elsif ( $opt->{verbose} ) { $line = ' ' . $line }
if ( $opt->{verbose} ) {
$line = "["
. $utils::config->{UI}->{$_}->{template} . "]"
. utils::fillwith( " ",
5 - length( $utils::config->{UI}->{$_}->{template} ) )
. $line;
}
say $line ;
}
exit(0);
}
if ( exists $opt->{addcheck} ) {
my $newchecks = eval( '[' . $opt->{addcheck} . ']' );
push(
@{ $utils::config->{UI}->{ $opt->{ui} }->{checks} },
eval( $opt->{addcheck} )
);
}
$utils::config->{version} = $version;
#use Data::Dumper; say Dumper($utils::config->{UI}->{$opt->{ui}}->{checks} );
##### LOOP #####
# Everyting is set.
# While the requested UI asks to continue, loop over it.
# when done - exit.
while ( $utils::config->{UI}->{ $opt->{ui} }
->loop( $utils::config, $opt->{ui}, $opt->{uiopts} ) eq "continue" )
{
utils::ErrLog "ui terminated but asks for a restart", "main", "INFO";
}
return 0;
}
# sometimes it's usefull to use this script as an include.
# if so, dont' run main straightaway but provide an import-function.
# this function can be tossed some args and will be mapped back to @ARGV.
unless (caller) {
main;
}
sub asImport {
@ARGV = @_;
main;
}