Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple remote process monitoring with Python

I'd like to write a python script to perform some very simple "agentless" monitoring of remote processes running on linux servers.

It would perform the following tasks, in psuedocode:

for each remoteIPAddress in listOfIPAddresses:
    log into server@remoteIPAddress via ssh
    execute the equivalent of a 'ps -ef' command
    grep the result to make sure a particular process (by name) is still running

One way to do this is to have python call shell scripts in a subprocess and parse their output. That seems pretty inefficient. Is there a better way to do this via python libraries?

All I could find via research here and elsewhere was:

  • psutil - looks like it doesn't do remote monitoring, so I'd have to run agents on the remote machines to report stats back via RPC.
  • pymeter - I would have to write my own plugin for monitoring a specific remote service.
  • stackoverflow #4546492 - Some helpful links but the poster was looking for a different solution.

Thanks, and please go easy on me, it's my first question :-)

like image 783
tohster Avatar asked Aug 11 '12 00:08

tohster


3 Answers

The Fabric library may be of interest to you.

like image 185
Carl Groner Avatar answered Oct 06 '22 00:10

Carl Groner


Check out paramiko. You can use it to ssh into the server and run commands. You can then parse the results and do what you'd like with them.

like image 40
Mark Hildreth Avatar answered Oct 06 '22 01:10

Mark Hildreth


Taking cues from the answers above, I investigated Fabric and found the following presentation particularly interesting/helpful. It is an overview of three libraries -- Fabric, Cuisine, and Watchdog -- for server monitoring and administration. For posterity:

Using Fabric, Cuisine, and Watchdog for server administration in Python

like image 26
tohster Avatar answered Oct 05 '22 23:10

tohster