diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2010-01-05 14:47:14 -0800 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2010-01-05 14:47:14 -0800 |
commit | 408dba64b3b1369b5986d369a816ae22ed2730b2 (patch) | |
tree | c8453f298d821571ac8a66a39421a91dce0dc667 | |
parent | 9c901a3be8d07d8c4e5375223d5acca39511798e (diff) | |
download | vyatta-cfg-system-408dba64b3b1369b5986d369a816ae22ed2730b2.tar.gz vyatta-cfg-system-408dba64b3b1369b5986d369a816ae22ed2730b2.zip |
Cleanup SNMP config file generation
Create to temporary file first, then move.
-rw-r--r-- | scripts/snmp/vyatta-snmp.pl | 97 |
1 files changed, 44 insertions, 53 deletions
diff --git a/scripts/snmp/vyatta-snmp.pl b/scripts/snmp/vyatta-snmp.pl index fcbeeb3f..1f4a3cfc 100644 --- a/scripts/snmp/vyatta-snmp.pl +++ b/scripts/snmp/vyatta-snmp.pl @@ -1,12 +1,12 @@ #!/usr/bin/perl # # Module: vyatta-snmp.pl -# +# # **** License **** # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -# +# # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -15,11 +15,11 @@ # This code was originally developed by Vyatta, Inc. # Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc. # All Rights Reserved. -# +# # Author: Stig Thormodsrud # Date: October 2007 # Description: Script to glue vyatta cli to snmp daemon -# +# # **** End License **** # @@ -27,6 +27,7 @@ use lib "/opt/vyatta/share/perl5/"; use Vyatta::Config; use Vyatta::Misc; use Getopt::Long; +use File::Copy; use strict; use warnings; @@ -34,6 +35,7 @@ use warnings; my $mibdir = '/opt/vyatta/share/snmp/mibs'; my $snmp_init = '/opt/vyatta/sbin/snmpd.init'; my $snmp_conf = '/etc/snmp/snmpd.conf'; +my $snmp_tmp = "/tmp/snmpd.conf.$$"; my $snmp_snmpv3_user_conf = '/usr/share/snmp/snmpd.conf'; my $snmp_snmpv3_createuser_conf = '/var/lib/snmp/snmpd.conf'; my $versionfile = '/opt/vyatta/etc/version'; @@ -47,11 +49,18 @@ sub snmp_stop { } sub snmp_start { - my $config; + open (my $fh, '>', $snmp_tmp) + or die "Couldn't open $snmp_tmp - $!"; + + select $fh; + snmp_get_constants(); + snmp_get_values(); + close $fh; + select STDOUT; + + move($snmp_tmp, $snmp_conf) + or die "Couldn't move $snmp_tmp to $snmp_conf - $!"; - $config = snmp_get_constants(); - $config .= snmp_get_values(); - snmp_write_file($config); snmp_restart(); } @@ -72,30 +81,26 @@ sub get_version { } sub snmp_get_constants { - my $output; my $version = get_version(); - - my $date = `date`; - chomp $date; - $output = "#\n# autogenerated by vyatta-snmp.pl on $date\n#\n"; - $output .= "sysDescr Vyatta $version\n"; - $output .= "sysObjectID 1.3.6.1.4.1.30803\n"; - $output .= "sysServices 14\n"; - $output .= "smuxpeer .1.3.6.1.4.1.3317.1.2.2\n"; # ospfd - $output .= "smuxpeer .1.3.6.1.4.1.3317.1.2.5\n"; # bgpd - $output .= "smuxpeer .1.3.6.1.4.1.3317.1.2.3\n"; # ripd - $output .= "smuxsocket localhost\n"; - $output .= "perl do \"/opt/vyatta/sbin/enterprise-mib.pl\"\n"; - return $output; + my $now = localtime; + + print "# autogenerated by vyatta-snmp.pl on $now\n"; + print "sysDescr Vyatta $version\n"; + print "sysObjectID 1.3.6.1.4.1.30803\n"; + print "sysServices 14\n"; + print "smuxpeer .1.3.6.1.4.1.3317.1.2.2\n"; # ospfd + print "smuxpeer .1.3.6.1.4.1.3317.1.2.5\n"; # bgpd + print "smuxpeer .1.3.6.1.4.1.3317.1.2.3\n"; # ripd + print "smuxsocket localhost\n"; + print "perl do \"/opt/vyatta/sbin/enterprise-mib.pl\"\n"; } sub snmp_get_values { - my $output = ''; my $config = new Vyatta::Config; $config->setLevel("protocols snmp community"); my @communities = $config->listNodes(); - + foreach my $community (@communities) { my $authorization = $config->returnValue("$community authorization"); my @clients = $config->returnValues("$community client"); @@ -103,26 +108,26 @@ sub snmp_get_values { if (scalar(@clients) == 0 and scalar(@networks) == 0){ if (defined $authorization and $authorization eq "rw") { - $output .= "rwcommunity $community\n"; + print "rwcommunity $community\n"; } else { - $output .= "rocommunity $community\n"; + print "rocommunity $community\n"; } } else { - if (scalar(@clients) != 0){ + if (scalar(@clients) != 0) { foreach my $client (@clients){ if (defined $authorization and $authorization eq "rw") { - $output .= "rwcommunity $community $client\n"; + print "rwcommunity $community $client\n"; } else { - $output .= "rocommunity $community $client\n"; + print "rocommunity $community $client\n"; } } } if (scalar(@networks) != 0){ foreach my $network (@networks){ if (defined $authorization and $authorization eq "rw") { - $output .= "rwcommunity $community $network\n"; + print "rwcommunity $community $network\n"; } else { - $output .= "rocommunity $community $network\n"; + print "rocommunity $community $network\n"; } } @@ -133,15 +138,17 @@ sub snmp_get_values { $config->setLevel("protocols snmp"); my $contact = $config->returnValue("contact"); if (defined $contact) { - $output .= "syscontact \"$contact\" \n"; + print "syscontact \"$contact\" \n"; } + my $description = $config->returnValue("description"); if (defined $description) { - $output .= "sysdescr \"$description\" \n"; + print "sysdescr \"$description\" \n"; } + my $location = $config->returnValue("location"); if (defined $location) { - $output .= "syslocation \"$location\" \n"; + print "syslocation \"$location\" \n"; } my @trap_targets = $config->returnValues("trap-target"); @@ -157,15 +164,13 @@ sub snmp_get_values { my $vyatta_user = "vyatta" . "$generate_vyatta_user_append_string"; snmp_create_snmpv3_user($vyatta_user); snmp_write_snmpv3_user($vyatta_user); - $output .= "iquerySecName $vyatta_user\n"; + print "iquerySecName $vyatta_user\n"; # code to activate link up down traps - $output .= "linkUpDownNotifications yes\n"; + print "linkUpDownNotifications yes\n"; } foreach my $trap_target (@trap_targets) { - $output .= "trap2sink $trap_target\n"; + print "trap2sink $trap_target\n"; } - - return $output; } sub snmp_create_snmpv3_user { @@ -188,14 +193,6 @@ sub snmp_write_snmpv3_user { close $fh; } -sub snmp_write_file { - my ($config) = @_; - - open(my $fh, '>', $snmp_conf) || die "Couldn't open $snmp_conf - $!"; - print $fh $config; - close $fh; -} - # # main @@ -208,9 +205,3 @@ GetOptions("update-snmp!" => \$update_snmp, snmp_start() if ($update_snmp); snmp_stop() if ($stop_snmp); - -exit 0; - - - - |