/
naglint
executable file
·312 lines (224 loc) · 8.18 KB
/
naglint
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
#!/bin/bash
# read rc files if exist
[ -e ~/.profile ] && . ~/.profile
[ -e ~/.thruk ] && . ~/.thruk
if [ -e $(dirname $0)/../lib/Thruk.pm ]; then
export PERL5LIB="$PERL5LIB:$(dirname $0)/../lib:$(dirname $0)/../plugins/plugins-available/conf/lib";
if [ "$OMD_ROOT" != "" -a "$THRUK_CONFIG" = "" ]; then export THRUK_CONFIG="$OMD_ROOT/etc/thruk"; fi
if [ -z $THRUK_CONFIG ]; then export THRUK_CONFIG="$(dirname $0)/../"; fi
elif [ ! -z $OMD_ROOT ]; then
export PERL5LIB=$OMD_ROOT/share/thruk/lib:$OMD_ROOT/share/thruk/plugins/plugins-available/conf/lib:$PERL5LIB
if [ -z $THRUK_CONFIG ]; then export THRUK_CONFIG="$OMD_ROOT/etc/thruk"; fi
else
export PERL5LIB=$PERL5LIB:/usr/share/thruk/lib:/usr/share/thruk/plugins/plugins-available/conf/lib:/usr/lib/thruk/perl5:/usr/lib64/thruk/perl5;
if [ -z $THRUK_CONFIG ]; then export THRUK_CONFIG='/etc/thruk'; fi
fi
eval 'exec perl -x $0 ${1+"$@"} ;'
if 0;
#! -*- perl -*-
# vim: expandtab:ts=4:sw=4:syntax=perl
#line 25
##############################################
use strict;
use warnings;
our $VERSION = '2.24';
my $branch = '';
$branch = Thruk::Config::get_git_name() unless $branch ne '';
my $naglint = Thruk::naglint->new();
exit $naglint->run();
##############################################
=head1 NAME
naglint - beautify nagios config files
=head1 SYNOPSIS
Usage: naglint [options] <file> [<files...>]
Options:
-h, --help Show this help message and exit
-v, --verbose Print verbose output
-V, --version Print version
-c, --config config file. Default is ~/.naglintrc
-i replace content inline instead of printing to stdout
-r read directories recursivly
--core core type can be nagios, icinga or shinken. Will be
autodetected unless specified.
=head1 DESCRIPTION
This script beautifies nagios config files.
- clean whitespace
- clean indention
- remove deprecated attributes
- validate duplicate or invalid attributes
- break long command lines into shorter pieces
- sort attributes naturally
=head1 OPTIONS
naglint has the following arguments:
=over 4
=item B<-h> , B<--help>
print help and exit
=item B<-v> , B<--verbose>
print verbose output too
=item B<-c> , B<--config>
use different config file. Default locations are ~/.naglintrc and /etc/thruk/naglint.conf
=item B<-V> , B<--version>
print version and exit
=item B<--core>
use specific config type. can be 'nagios', 'icinga' or 'shinken'.
enables core type specific attributes. Will be autodetected unless
specified.
=item B<-i>
edit given files inline and overwrite them with their beautified objects
=item B<-r>
read directories recursivly
=back
=head1 RETURN VALUE
returns 0 on success or number of errors otherwise
=head1 CONFIG
see FILES section for where to store config options.
=over
=item indent_object_key
Number of spaces before object attribute names. Default 2
=item indent_object_value
Number of spaces before objects attribute values. Default 30
=item indent_object_comments
Number of spaces before inline comments. Default 68
=item list_join_string
Should list attributes be joined with space or not. Default ','
=item break_long_arguments
Should long arguments be broken into multiple lines. Default 1
=item object_attribute_key_order
list of attributes which should be on top of the object
=item object_cust_var_order
list of custom variables which will be put on top of other custom variables
=back
=head1 EXAMPLES
Beautify single config file
%> naglint objects.cfg > beauty.cfg
Beautify and replace single config file inline
%> naglint -i objects.cfg
Process objects from STDIN
%> cat objects.cfg | naglint > beauty.cfg
=head1 FILES
~/.naglintrc
user config file
/etc/thruk/naglint.conf
global config file
=head1 AUTHOR
2012, Sven Nierlein, <sven@nierlein.de>
=cut
##############################################
package Thruk::naglint;
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Thruk::Config;
use Thruk::Utils::Scripts;
use Monitoring::Config;
use Monitoring::Config::File;
##############################################
sub new {
my($class) = @_;
my $self = {};
bless $self, $class;
return $self;
}
##############################################
sub run {
my($self) = @_;
$self->{'opt'} = {
'verbose' => 0,
'files' => [],
'inline' => 0,
'coretype' => 'any',
'config' => '',
};
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('bundling');
GetOptions (
"h|help" => \$self->{'opt'}->{'help'},
"v|verbose" => \$self->{'opt'}->{'verbose'},
"V|version" => \$self->{'opt'}->{'version'},
"config=s" => \$self->{'opt'}->{'config'},
"c|core=s" => \$self->{'opt'}->{'coretype'},
"i" => \$self->{'opt'}->{'inline'},
"r" => \$self->{'opt'}->{'recursive'},
"<>" => sub { push @{$self->{'opt'}->{'files'}}, $_[0] },
) or _usage('error in options');
if($self->{'opt'}->{'version'}) { print "Thruk Version $VERSION".($branch ne '' ? '~'.$branch : '')."\n"; exit 0; }
pod2usage( { -verbose => 2, -exit => 3 } ) if $self->{'opt'}->{'help'};
# do we have a rc file?
Monitoring::Config->read_rc_file($self->{'opt'}->{'config'});
##############################################
# make sure we don't touch owner and permissions
local $ENV{'THRUK_NO_TOUCH_PERM'} = 1;
my $errors = 0;
# attached to a terminal?
if (!-t STDIN and scalar @{$self->{'opt'}->{'files'}} == 0) {
my $file = Monitoring::Config::File->new('virt.cfg', [], $self->{'opt'}->{'coretype'});
my $text = "";
while(<STDIN>) {
$text .= $_;
if($_ =~ m/^\s*}\s*$/mx) {
$file->update_objects_from_text($text);
$errors += Thruk::Utils::Scripts::print_errors($file);
print $file->get_new_file_content();
$text = '';
}
}
$file->update_objects_from_text($text);
$errors += Thruk::Utils::Scripts::print_errors($file);
print $file->get_new_file_content();
exit $errors;
}
_usage('no input files!') if scalar @{$self->{'opt'}->{'files'}} == 0;
# iterate all given files
for my $filename (@{$self->{'opt'}->{'files'}}) {
$errors += $self->_process_file($filename);
}
return $errors;
}
##############################################
# SUBS
##############################################
sub _usage {
my($msg) = @_;
print $msg."\n" if $msg;
print "Usage: naglint [options] <file> [<files...>]\n";
print "Detailed help available with --help\n";
exit;
}
##############################################
sub _process_file {
my($self, $filename) = @_;
if(!-e $filename) {
print STDERR "ERROR: ", $filename, " ", $!, "\n";
return 1;
}
if(-d $filename) {
if(!$self->{'opt'}->{'recursive'}) {
print STDERR "ERROR: won't process directory unless -r specified\n";
return 1;
} else {
my $errors = 0;
my $mc = Monitoring::Config->new();
my $files = $mc->_get_files_for_folder($filename);
for my $f (@{$files}) {
$errors += $self->_process_file($f);
}
return $errors;
}
}
print STDERR "parsing ", $filename, "\n" if $self->{'opt'}->{'verbose'};
my $file = Monitoring::Config::File->new($filename, [], $self->{'opt'}->{'coretype'});
die("could not create file object") unless defined $file;
$file->update_objects();
$file->{'changed'} = 1; # new content will only be returned for changed files
return 1 if Thruk::Utils::Scripts::print_errors($file);
if($self->{'opt'}->{'inline'}) {
$file->save();
print STDERR "wrote ", $filename, "\n";
} else {
print $file->get_new_file_content();
}
return 0;
}
##############################################
1;