From 1d3e6b3ea190d14b68b2f9792784037d4f12928b Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <stephen.hemminger@vyatta.com>
Date: Sat, 9 Oct 2010 11:15:13 -0700
Subject: Check if SNMP is running before starting

The SNMPD restart has 2 or 3 seconds of sleep that
is unnecessary if SNMPD isn't running.
---
 scripts/snmp/vyatta-snmp.pl | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/scripts/snmp/vyatta-snmp.pl b/scripts/snmp/vyatta-snmp.pl
index e077482c..e977d619 100644
--- a/scripts/snmp/vyatta-snmp.pl
+++ b/scripts/snmp/vyatta-snmp.pl
@@ -45,8 +45,16 @@ my $local_agent = 'unix:/var/run/snmpd.socket';
 
 my $snmp_level = 'service snmp';
 
-sub snmp_restart {
-    system("$snmp_init restart > /dev/null 2>&1 &");
+sub snmp_running {
+    open (my $pidf, '<', "/var/run/snmpd.pid")
+	or return;
+    my $pid = <$pidf>;
+    close $pidf;
+
+    chomp $pid;
+    my $exe = readlink "/proc/$pid/exe";
+
+    return (defined($exe) && $exe eq "/usr/sbin/snmpd");
 }
 
 sub snmp_stop {
@@ -69,7 +77,11 @@ sub snmp_start {
     move($snmp_tmp, $snmp_conf)
 	or die "Couldn't move $snmp_tmp to $snmp_conf - $!";
 
-    snmp_restart();
+    if (snmp_running()) {
+	system("$snmp_init restart > /dev/null 2>&1 &");
+    } else {
+	system("$snmp_init start > /dev/null 2>&1 &");
+    }
 }
 
 sub get_version {
-- 
cgit v1.2.3