LinuxSay - A Discussion Forum for Linux Enthusiasts

Python Linux monitoring tool

Hi everybody.I want to share my new Python Linux monitoring tool.

Original Code: https://secflaw.wordpress.com/2015/05/20/python-monitoring-tool/

#!/usr/bin/python # -*- coding: utf8 -*- # Monitoring Tool # Python 2.7 # __author__ = 'camoufl4g3'

print “”"
##########################################################

Linux server monitoring tool

#camoufl4g3

##########################################################

     """

import socket

import urllib,os,platform,netifaces,ipgetter,subprocess

## Internet Connection

try :
stri = “https://www.google.com
data = urllib.urlopen(stri)
print “Internet: Connected”
except e:
print “Internet: not connected” ,e

print ‘-----------------------------------------------------------------------------’

## Os

print "OS name: ", platform.uname()[0]

print ‘-----------------------------------------------------------------------------’

#Architecture

print “Architecture:” , platform.uname()[4]

print ‘-----------------------------------------------------------------------------’

## Kernel Release

print “Kernel Release:” , platform.uname()[2]

print ‘-----------------------------------------------------------------------------’

## Hostname

print "Hostname: ",socket.gethostname()

netifaces.ifaddresses(‘eth0’)
ip = netifaces.ifaddresses(‘eth0’)[2][0][‘addr’]

print ‘-----------------------------------------------------------------------------’

print "Local Ip : ",ip

print ‘-----------------------------------------------------------------------------’

## Internal Ip

print "External Ip: ",ipgetter.myip()

print ‘-----------------------------------------------------------------------------’

#print platform.uname()

## Logged Users

print "Logged Users: " + " \n ",subprocess.check_output(“who”)

print ‘-----------------------------------------------------------------------------’

##RAM and SWAP Usages

print “Check RAM and SWAP Usages: “,”\n”

print ‘-----------------------------------------------------------------------------’

print subprocess.call([‘free’, ‘-h’])

print ‘-----------------------------------------------------------------------------’

## Disk usage

print “Disk usage: “,”\n”

print ‘-----------------------------------------------------------------------------’

print subprocess.call([‘df’, ‘-h’])

print ‘-----------------------------------------------------------------------------’

## Uptime

print “Uptime: “,”\n”

print ‘-----------------------------------------------------------------------------’

print subprocess.call([‘uptime’])

print ‘-----------------------------------------------------------------------------’

1 Like

Hi @camoufl4g3

Sorry, mate, but I can’t figure out a thing form your screenshot (must be my laptop’s 15" screen). Could you post an imgur or sth link?

Hi @BrankoTesla

No problem bro.I pasted it to pastebin.

http://pastebin.com/5pMhxr9g

1 Like

@camoufl4g3
Yes, the image screenshot you posted was not clear, can you post the higher quality one with big size…

@ravisaive

@camoufl4g3
The script not working for me, I am getting error as:

[root@test tecmint]# **python monitor.py**

##########################################################

Linux server monitoring tool

#camoufl4g3

##########################################################

Traceback (most recent call last):
File “monitor.py”, line 21, in
import urllib,os,platform,netifaces,ipgetter,subprocess
ImportError: No module named netifaces

@ravisaive

bro solve it as

sudo pip install netifaces

@camoufl4g3
That’s great you’ve created the monitor script, but you should also show instructions for per-requisites for this monitoring tool… Now I am getting error like command ‘pip’ not found, I try to install pip on my Linux Mint, but giving error “Unable to locate package pip”. Finally I found a way to install pip and netifaces as suggested below.

 # sudo apt-get install python-pip
 # sudo pip install netifaces

After that I am still getting the error.

tecmint@tecmint ~ $ python monitor.py

 ##########################################################
 #                                                        #
 #             Linux server monitoring tool               #
 #                   #camoufl4g3                          #
 #                                                        #
 #                                                        #
 ##########################################################
 
         
Traceback (most recent call last):
  File "monitor.py", line 21, in 
    import urllib,os,platform,netifaces,ipgetter,subprocess
ImportError: No module named ipgetter

Thanks! Looks much better now! :blush:

thanks dude.when which module gives error try sudo pip install module_name.
you try now to fix ipgetter modul such
sudo pip install ipgetter

@camoufl4g3,
I have written a similar script explicitly in Shell Scripting Language months ago. It has certain features like it can be installed and can be called from anywhere using command-line switches. The script has minimal installation requirement and can be extended to any length. Though the script is not very neat as of now, I will be working on it and extend it.

Link to Post: http://www.tecmint.com/linux-server-health-monitoring-script/

Download script as: wget http://tecmint.com/wp-content/scripts/tecmint_monitor.sh

1 Like

@camoufl4g3
Thanks for your quick response, yes I was able run the script now without any error, here is the output of the script under my Linux Mint 17.1.

This is really an fundamental script for python script-kidding like me. Is there any way to automatically detect up-ing interface, because not alway ‘eth0’ is on UP.