File Coverage

blib/lib/Ixchel/Actions/snmp_service.pm
Criterion Covered Total %
statement 17 67 25.3
branch 0 30 0.0
condition 0 3 0.0
subroutine 6 10 60.0
pod 2 4 50.0
total 25 114 21.9


line stmt bran cond sub pod time code
1             package Ixchel::Actions::snmp_service;
2              
3 1     1   104973 use 5.006;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         35  
5 1     1   3 use warnings;
  1         2  
  1         49  
6 1     1   440 use Rex::Commands::Gather;
  1         193406  
  1         7  
7 1     1   1163 use Rex::Commands::Service;
  1         5715  
  1         13  
8 1     1   191 use base 'Ixchel::Actions::base';
  1         2  
  1         41  
9              
10             # prevents Rex from printing out rex is exiting after the script ends
11             $::QUIET = 2;
12              
13             =head1 NAME
14              
15             Ixchel::Actions::snmp_service - Manage the snmpd service.
16              
17             =head1 VERSION
18              
19             Version 0.1.1
20              
21             =cut
22              
23             our $VERSION = '0.1.1';
24              
25             =head1 CLI SYNOPSIS
26              
27             ixchel -a snmp_service --enable [B<--start>|B<--stop>|B<--restart>|B<--stopstart>]
28              
29             ixchel -a snmp_service --disable [B<--start>|B<--stop>|B<--restart>|B<--stopstart>]
30              
31             ixchel -a snmp_service --start
32              
33             ixchel -a snmp_service --stop
34              
35             ixchel -a snmp_service --restart
36              
37             ixchel -a snmp_service --stopstart
38              
39             =head1 CODE SYNOPSIS
40              
41             use Data::Dumper;
42              
43             my $results=$ixchel->action(action=>'snmp_enable', opts=>{enable=>1,start=>1});
44              
45             =head1 FLAGS
46              
47             =head2 --enable
48              
49             Enable the service.
50              
51             My not be combined with --disable.
52              
53             =head2 --disable
54              
55             Disable the service.
56              
57             My not be combined with --enable.
58              
59             =head2 --start
60              
61             Start the service.
62              
63             May not be combined with.
64              
65             --start
66             --stop
67             --restart
68             --stopstart
69              
70             =head2 --stop
71              
72             Stop the service.
73              
74             May not be combined with.
75              
76             --start
77             --stop
78             --restart
79             --stopstart
80              
81             =head2 --restart
82              
83             Restart the service.
84              
85             May not be combined with.
86              
87             --start
88             --stop
89             --restart
90             --stopstart
91              
92             =head2 --stopstart
93              
94             Stop and then restart the service.
95              
96             May not be combined with.
97              
98             --start
99             --stop
100             --restart
101             --stopstart
102              
103             =head1 RESULT HASH REF
104              
105             .errors :: A array of errors encountered.
106             .status_text :: A string description of what was done and the results.
107             .ok :: Set to zero if any of the above errored.
108              
109             =cut
110              
111       0 0   sub new_extra { }
112              
113             sub action_extra {
114 0     0 0   my $self = $_[0];
115              
116             # make sure we don't have extra start/stop stuff specified
117 0           my $extra_opts = 0;
118 0           my @various_opts = ( 'restart', 'start', 'stop', 'stopstart' );
119 0           foreach my $item (@various_opts) {
120 0 0         if ( defined( $self->{opts}{$item} ) ) {
121 0           $extra_opts++;
122             }
123             }
124 0 0         if ( $extra_opts > 1 ) {
125 0           my $extra_opts_string = '--' . join( ', --', @various_opts );
126 0           $self->status_add( error => 1, status => $extra_opts_string . ' can not be combined' );
127 0           return undef;
128             }
129              
130             # make sure --enable and --disable are not both specified
131 0 0 0       if ( $self->{opts}{enable} && $self->{opts}{disable} ) {
132 0           $self->status_add( error => 1, status => '--disable and --enable may not be specified at the same time' );
133 0           return undef;
134             }
135              
136             # enable/disable it
137 0 0         if ( $self->{opts}{enable} ) {
    0          
138 0           eval {
139 0           service 'snmpd', ensure => 'started';
140 0           $self->status_add( status => 'snmpd enabled' );
141             };
142 0 0         if ($@) {
143 0           $self->status_add( status => 'Errored enabling snmpd... ' . $@ );
144             }
145             } elsif ( $self->{opts}{disable} ) {
146 0           eval {
147 0           service 'snmpd', ensure => 'stopped';
148 0           $self->status_add( status => 'snmpd disabled' );
149             };
150 0 0         if ($@) {
151 0           $self->status_add( error => 1, status => 'Errored disabling snmpd... ' . $@ );
152             }
153             }
154              
155             # start/stop it etc
156 0 0         if ( $self->{opts}{restart} ) {
    0          
    0          
    0          
157 0           eval {
158 0           service 'snmpd' => 'restart';
159 0           $self->status_add( status => 'snmped restarted' );
160             };
161 0 0         if ($@) {
162 0           $self->status_add( error => 1, status => 'Errored restarting snmpd... ' . $@ );
163             }
164             } elsif ( $self->{opts}{start} ) {
165 0           eval {
166 0           service 'snmpd' => 'start';
167 0           $self->status_add( status => 'snmped started' );
168             };
169 0 0         if ($@) {
170 0           $self->status_add( error => 1, status => 'Errored starting snmpd... ' . $@ );
171             }
172             } elsif ( $self->{opts}{stop} ) {
173 0           eval {
174 0           service 'snmpd' => 'stop';
175 0           $self->status_add( status => 'snmped stopped' );
176             };
177 0 0         if ($@) {
178 0           $self->status_add( error => 1, status => 'Errored stopping snmpd... ' . $@ );
179             }
180             } elsif ( $self->{opts}{stopstart} ) {
181 0           eval {
182 0           service 'snmpd' => 'stop';
183 0           $self->status_add( status => 'snmped stopped' );
184 0           service 'snmpd' => 'start';
185 0           $self->status_add( status => 'snmped started' );
186             };
187 0 0         if ($@) {
188 0           $self->status_add( error => 1, status => 'Errored stopping and then starting snmpd... ' . $@ );
189             }
190             } ## end elsif ( $self->{opts}{stopstart} )
191              
192 0           return undef;
193             } ## end sub action_extra
194              
195             sub short {
196 0     0 1   return 'Manage the snmpd service.';
197             }
198              
199             sub opts_data {
200 0     0 1   return '
201             enable
202             disable
203             start
204             stop
205             restart
206             stopstart
207             ';
208             } ## end sub opts_data
209              
210             1;