{"id":1376,"date":"2014-02-03T16:43:13","date_gmt":"2014-02-03T15:43:13","guid":{"rendered":"http:\/\/www.wmaus.net\/?p=1376"},"modified":"2014-02-03T17:04:29","modified_gmt":"2014-02-03T16:04:29","slug":"remotely-restart-the-snmp-daemon-on-ubuntu-12-04-lts-via-a-python-skript","status":"publish","type":"post","link":"https:\/\/www.wmaus.net\/?p=1376","title":{"rendered":"Remotely restart the SNMP daemon on Ubuntu 12.04 LTS via a Python skript"},"content":{"rendered":"<p>It can happen that the SNMP daemon on a server crashes or stops, sometimes without leaving any hints in the system&#8217;s logfiles. If you&#8217;re running a monitoring server like WhatsUp, you can define a monitor for the SNMP service and should it be reported as down, you can define an action that launches the following Python skript to restart the daemon:<\/p>\n<pre lang=\"python\">#!\/usr\/bin\/python\r\nimport paramiko\r\n\r\nssh = paramiko.SSHClient()\r\nssh.set_missing_host_key_policy( paramiko.AutoAddPolicy())\r\nssh.connect('yourserver.yourdomain.net', username='pythonuser', password='password')\r\n\r\nstdin, stdout, stderr = ssh.exec_command( \"sudo service snmpd restart\")\r\n\r\nssh.close()<\/pre>\n<p>In order for this skript to work properly, the user &#8220;pythonuser&#8221; needs to be able to execute sudo commands without having to type in a password again. The used Python ssh-library, paramiko, does not easily support the use of sudo commands, so it is easier to work around the problem by allowing the user &#8220;pythonuser&#8221; to run certain commands without having to provide a password again.<\/p>\n<p>In order to allow &#8220;pythonuser&#8221; to run the &#8220;service&#8221; command via &#8220;sudo&#8221; without entering an additional password, this line has to be added to the end of \/etc\/sudoers on the server on which you need to restart the snmp daemon:<\/p>\n<pre>pythonuser ALL=NOPASSWD: \/usr\/sbin\/service<\/pre>\n<p>IMPORTANT: This line MUST be at the end of the file, otherwise it will NOT override any settings defined for the &#8220;sudo&#8221; or other user groups (whose members must provide passwords).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It can happen that the SNMP daemon on a server crashes or stops, sometimes without leaving any hints in the system&#8217;s logfiles. If you&#8217;re running a monitoring server like WhatsUp, you can define a monitor for the SNMP service and should it be reported as down, you can define an action that launches the following &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.wmaus.net\/?p=1376\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Remotely restart the SNMP daemon on Ubuntu 12.04 LTS via a Python skript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[],"class_list":["post-1376","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.wmaus.net\/index.php?rest_route=\/wp\/v2\/posts\/1376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wmaus.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wmaus.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wmaus.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wmaus.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1376"}],"version-history":[{"count":5,"href":"https:\/\/www.wmaus.net\/index.php?rest_route=\/wp\/v2\/posts\/1376\/revisions"}],"predecessor-version":[{"id":1381,"href":"https:\/\/www.wmaus.net\/index.php?rest_route=\/wp\/v2\/posts\/1376\/revisions\/1381"}],"wp:attachment":[{"href":"https:\/\/www.wmaus.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wmaus.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wmaus.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}