Debian Codegraph @

check_mysql_health/check_mysql_health-2.2.2/plugins-scripts/Nagios/Extraopts.pm

raw
1 package Extraopts;
2
3 use strict;
4 use File::Basename;
5 use Data::Dumper;
6
7 sub new {
8 my $class = shift;
9 my %params = @_;
10 my $self = {
11 file => $params{file},
12 commandline => $params{commandline},
13 config => {},
14 section => 'default_no_section',
15 };
16 bless $self, $class;
17 $self->prepare_file_and_section();
18 $self->init();
19 return $self;
20 }
21
22 sub prepare_file_and_section {
23 my $self = shift;
24 if (! defined $self->{file}) {
25 # ./check_stuff --extra-opts
26 $self->{section} = basename($0);
27 $self->{file} = $self->get_default_file();
28 } elsif ($self->{file} =~ /^[^@]+$/) {
29 # ./check_stuff --extra-opts=special_opts
30 $self->{section} = $self->{file};
31 $self->{file} = $self->get_default_file();
32 } elsif ($self->{file} =~ /^@(.*)/) {
33 # ./check_stuff --extra-opts=@/etc/myconfig.ini
34 $self->{section} = basename($0);
35 $self->{file} = $1;
36 } elsif ($self->{file} =~ /^(.*?)@(.*)/) {
37 # ./check_stuff --extra-opts=special_opts@/etc/myconfig.ini
38 $self->{section} = $1;
39 $self->{file} = $2;
40 }
41 }
42
43 sub get_default_file {
44 my $self = shift;
45 foreach my $default (qw(/etc/nagios/plugins.ini
46 /usr/local/nagios/etc/plugins.ini
47 /usr/local/etc/nagios/plugins.ini
48 /etc/opt/nagios/plugins.ini
49 /etc/nagios-plugins.ini
50 /usr/local/etc/nagios-plugins.ini
51 /etc/opt/nagios-plugins.ini)) {
52 if (-f $default) {
53 return $default;
54 }
55 }
56 return undef;
57 }
58
59 sub init {
60 my $self = shift;
61 if (! defined $self->{file}) {
62 $self->{errors} = sprintf 'no extra-opts file specified and no default file found';
63 } elsif (! -f $self->{file}) {
64 $self->{errors} = sprintf 'could not open %s', $self->{file};
65 } else {
66 my $data = do { local (@ARGV, $/) = $self->{file}; <> };
67 my $in_section = 'default_no_section';
68 foreach my $line (split(/\n/, $data)) {
69 if ($line =~ /\[(.*)\]/) {
70 $in_section = $1;
71 } elsif ($line =~ /(.*?)\s*=\s*(.*)/) {
72 $self->{config}->{$in_section}->{$1} = $2;
73 }
74 }
75 }
76 }
77
78 sub is_valid {
79 my $self = shift;
80 return ! exists $self->{errors};
81 }
82
83 sub overwrite {
84 my $self = shift;
85 my %commandline = ();
86 if (scalar(keys %{$self->{config}->{default_no_section}}) > 0) {
87 foreach (keys %{$self->{config}->{default_no_section}}) {
88 $commandline{$_} = $self->{config}->{default_no_section}->{$_};
89 }
90 }
91 if (exists $self->{config}->{$self->{section}}) {
92 foreach (keys %{$self->{config}->{$self->{section}}}) {
93 $commandline{$_} = $self->{config}->{$self->{section}}->{$_};
94 }
95 }
96 foreach (keys %commandline) {
97 if (! exists $self->{commandline}->{$_}) {
98 $self->{commandline}->{$_} = $commandline{$_};
99 }
100 }
101 }
102
103