install.pl untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 212 CC 29 SCAR 31.0
CC
29
Cov
0%
configure_snmp
56.0
get_perl_version
42.0
install_modules
30.0
CRAP 22.2
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4  
5 #BEGIN PRIVATE GLOBAL CONFIGURATION
6 my $base_dir = `pwd`;
7 chomp $base_dir;
8 my $build_dir = $base_dir . '/build';
9  
10 print $build_dir . "\n";
11  
12 my $mod_root_loc = 'http://backpan.perl.org/authors/id/H/HA/HARDAKER/';
13 my @req_modules = (
14 'SNMP-5.0301002.tar.gz',
15 'NetSNMP-default_store-5.0301.tar.gz',
16 'NetSNMP-OID-5.0301.tar.gz',
17 'NetSNMP-agent-5.0301.tar.gz',
18 'NetSNMP-ASN-5.0301.tar.gz',
19  
20 );
21  
22 my @snmp_packages = (
23 'net-snmp',
24 'net-snmp-libs',
25 'net-snmp-config',
26 'net-snmp-devel',
27 'net-snmp-utils',
28 );
29
30 my @cpan_modules = (
31 'Module::Pluggable',
32 'parent',
33 'common::sense',
34 );
35  
36 my $snmp_config = '/etc/snmp/snmpd.conf';
37 my $perl = get_perl_version();
38  
39 my $monitor_app_name = 'mail_queue_monitor.pl';
40 my $monitor_app__install_path = '/usr/share/snmp/';
41 my $monitor_app = '/usr/share/snmp/mail_queue_monitor.pl';
42  
43 #set level of comfort messages.
44 my $debugging = 2;
45  
46 #System commands:
47 my $restart_snmpd = '/etc/init.d/snmpd restart';
48  
49  
50 make_temp_install_dir();
51 install_snmp_deps();
52 download_modules();
53 extract_modules();
54 install_modules();
55 install_cpan_modules();
56 install_snmp_monitor();
57 configure_snmp(is_snmp_configured());
58  
59 #TODO:
60 #configure_iptables unless iptables_is_configured();
61 cleanup();
62  
63 sub get_perl_version {
64 my $perl_ver;
65 print "Searching for Perl compiled with multithreading\n" if $debugging;
66 use Config;
67 if ($Config{usethreads}) {
68 print "Base Perl version uses threading support\n";
69 $perl_ver = 'perl';
70 }
71 else {
72 $_ = `whereis perl`;
73 my $perl_ver = $2 if m/(.*)(perl\.\d{2,})(.*)/;
74 print "Perl version $perl_ver\n";
75 if ($perl_ver) {
76 print "Perl found $perl_ver\n" if $debugging;
77 }
78 else {
79 die "Perl with multithreading support not found\n";
80 }
81 }
82 return $perl_ver;
83 }
84  
85 sub install_cpan_modules {
86 system "cpan $_" foreach @cpan_modules;
87 }
88  
89 sub install_snmp_monitor {
90 chdir $base_dir;
91 print `perl Makefile.PL`;
92 print `make`;
93 print `make test`;
94 print `make install`;
95 }
96  
97 sub install_snmp_deps {
98 print "Installing net-snmp and dependancies\n" if $debugging;
99 my $yum = `yum -y install $_` foreach @snmp_packages;
100 print $yum if $debugging > 1;
101 }
102  
103 sub make_temp_install_dir {
104 print "Making temporary install directory\n" if $debugging > 1;
105 mkdir $build_dir, 0755 unless (-d $build_dir);
106 }
107  
108 sub download_modules {
109 print "Downloading Necessary Modules\n" if $debugging > 1;
110 chdir $build_dir;
111 foreach (@req_modules) {
112 system 'wget ' . $mod_root_loc . $_;
113 }
114 }
115  
116 sub extract_modules {
117 print "Extracting Necessary Modules\n" if $debugging > 1;
118 chdir $build_dir;
119 foreach (@req_modules) {
120 system "tar xzf $_";
121 }
122 }
123  
124 sub export_env_vars {
125 print "Exporting NETSNMP_DONT_CHECK_VERSION=1\n" if $debugging > 1;
126 $ENV{NETSNMP_DONT_CHECK_VERSION} = 1;
127 }
128  
129 sub install_modules {
130 print "Installing Necessary Modules\n" if $debugging > 1;
131 export_env_vars;
132 foreach (@req_modules) {
133 print "returning to $build_dir\n" if $debugging > 1;
134 chdir $build_dir;
135 s/(.*).tar.gz/$1/mgsx;
136 print "entering extracted dir $_\n" if $debugging > 1;
137 chdir $_;
138 print "Installing $_\n" if $debugging > 1;
139 system "$perl Makefile.PL";
140 system 'make';
141 system 'make test';
142 system 'make install';
143 }
144 }
145  
146 sub is_snmp_configured {
147 open my $FHIN, '<', $snmp_config;
148 my ($access_found, $monitor_found, @configure);
149 foreach (<$FHIN>){
150 $access_found += 1 if m/rocommunity IPMONITOR 72.18.140.178/;
151 $monitor_found += 1 if m{use SNMPMonitor};
152 }
153 push @configure, 'access' unless $access_found;
154 push @configure, 'monitor' unless $monitor_found;
155 return @configure;
156 }
157  
158 sub configure_snmp {
159 my $access_list = get_access_list();
160 my $monitor_directives = get_monitor_directives();
161 return unless @_;
162  
163 my @to_configure = @_;
164  
165 open my $FHOUT, '>>', $snmp_config;
166 foreach (@to_configure){
167 if ($_ eq 'access'){
168 print "Appending $snmp_config with the access_list\n"
169 if $debugging > 1;
170 print $FHOUT $access_list;
171 }
172 elsif ($_ eq 'monitor'){
173 print "Appending $snmp_config with the monitor directives\n"
174 if $debugging > 1;
175 print $FHOUT $monitor_directives;
176 }
177 }
178 print "SNMP Configured, restarting snmpd\n";
179 my $ret_val = `/etc/init.d/snmpd restart`;
180 print $ret_val if $debugging > 1;
181 }
182  
183  
184 sub configure_iptables {
185  
186 }
187  
188 sub iptables_is_configured {
189  
190 }
191  
192 sub install_monitor_application {
193 use File::Copy ("cp");
194 cp ($monitor_app_name, $monitor_app);
195 }
196  
197 sub install_snmp_perl {
198 open my $FHOUT, '>', '/usr/share/snmp/snmp_perl.pl';
199 my $a =<<'END';
200 ##
201 ## SNMPD perl initialization file.
202 ##
203  
204 use NetSNMP::agent;
205 $agent = new NetSNMP::agent('dont_init_agent' => 1,
206 'dont_init_lib' => 1);
207 END
208 print $FHOUT $a;
209  
210 }
211  
212 sub cleanup {
213 if ($debugging > 2) {
214 print "Cleaning up after the installation\n" if $debugging;
215 system "rm -vrf $build_dir";
216 }
217 else {
218 system "rm -rf $build_dir";
219 }
220 }
221  
222  
223  
224 ################################################################################
225 ################################################################################
226 # subs to return file values
227  
228 sub get_access_list {
229 my $access_list = <<'END_ACCESS_LIST';
230 ###
231 # Access List
232 # inserted from auto_snmp_monitor_config.pl
233 ###
234  
235 # Lock access for the community IPMONITOR to ipMonitor's IP address
236 rocommunity IPMONITOR 72.18.140.178
237  
238 # create a debug user accessable only from this machine.
239 rocommunity debug localhost
240  
241 END_ACCESS_LIST
242  
243 return $access_list;
244  
245 }
246  
247 sub get_monitor_directives {
248 my $monitor_directives = <<'END_MONITOR_DIRECTIVES';
249 ###
250 # Custom Monitor Directives
251 # inserted from auto_snmp_monitor_config.pl
252 ###
253  
254 # Create and register our SMTP monitor
255  
256 perl use SNMPMonitor;
257 perl my $monitor = SNMPMonitor->new;
258  
259 END_MONITOR_DIRECTIVES
260 return $monitor_directives;
261 }