Home > Software engineering >  how to check service running on other server with python
how to check service running on other server with python

Time:03-09

I have a problem with checking my service on other windows or Linux servers.

My problem is that I have to make a request from one server to the other servers and check if the vital services of those servers are active or disabled.

I wrote Python code to check for services, which only works on a local system.

import psutil
 
def getService(name):

    service = None
    try:
        service = psutil.win_service_get(name)
        service = service.as_dict()
    except Exception as ex:
        print(str(ex))
    return service

service = getService('LanmanServer')
if service:
    print("service found")
else:
    print("service not found")

if service and service['status'] == 'running':
    print("service is running")
else:
    print("service is not running")

Does this code have this feature? Or suggest another code؟

I have reviewed suggestions such as using server agents (influx, ...), which are not working for my needs.

CodePudding user response:

You can do it simply with this script, this python script will send the status to the email that you want.
Import these lines :

from urllib2 import urlopen
from socket import socket
from sys import argv

And then use these codes :

def tcp_test(server_info):
cpos = server_info.find(':')
try:
    sock = socket()
    sock.connect((server_info[:cpos], int(server_info[cpos 1:])))
    sock.close
    return True
except:
    return False



def http_test(server_info):
try:
    data = urlopen(server_info).read()
    return True
except:
    return False


def server_test(test_type, server_info):
if test_type.lower() == 'tcp':
    return tcp_test(server_info)
elif test_type.lower() == 'http':
    return http_test(server_info)


def send_error(test_type, server_info, email_address):
  subject = '%s: %s %s error' % (asctime(), test_type.upper(), 
  server_info)
  message = 'There was an error while executing a %s test against 
  %s.' % (test_type.upper(), server_info)
  system('echo "%s" | mail -s "%s" %s' % (message, subject, 
  email_address))


if __name__ == '__main__':
if len(argv) != 4:
    print('Wrong number of arguments.')
elif not server_test(argv[1], argv[2]):
    send_error(argv[1], argv[2], argv[3])

CodePudding user response:

As far as I know, psutil can only be used for gathering information about local processes, and is not suitable for retrieving information about processes running on other hosts. If you want to check whether or not a process is running on another host, there are many ways to approach this problem, and the solution depends on how deep you want to go (or need to go), and what your local situation is. From the top of my head, here are some ideas:

If you are only dealing with network services with exposed ports:

  • A very simple solution would involve using a script and a port scanner (nmap); if a port that a service is listening behind, is open, then we can assume that the service is running. Run the script every once in a while to check up on the services, and do your thing.

  • If you want to stay in Python, you can achieve the same end result by using Python's socket module to try and connect to a given host and port to determine whether or not the port that a service is listening behind, is open.

  • A Python package or tool for monitoring network services on other hosts like this probably already exists.

If you want more information and need to go deeper, or you want to check up on local services, your solution will have to involve a local monitor process on each host, and connecting to that process to gather information.

  • You can use your code to implement a server that lets clients connect to it, to check up on the services running on that host. (Check the socket module's official documentation for examples on how to implement clients and servers.)

Here's the big thing though. Based on your question and how it was asked, I would assume that you do not have the experience nor the insight to implement this in a secure way yet. If you're using this for a simple hobby/student project, roll out your own solution, and learn. Otherwise, I would recommend that you check out an existing solution like Nagios, and follow the security recommendations very closely.

  • Related