Python Script Documetation
Python Script to get the server details
Prerequisite:
1. Install python3 and pip3 in your server
2. Install required python modules by using below command.
- sudo pip3 install ping3
3.The required modules are argparse, socket, time, json, datetime, platform, psutil, requests, pprint, uuid, os, ping3
How to execute the script:
First we need to save the file in .py(dot py) format for example monitor.py
Then we can execute it by using python3 command:
- sudo python3 monitor.py
If we need output of the script as a text file use below command:
- sudo python3 monitor.py > monitor_result.txt
It will create a monitor_result.txt file and save the file in the current directory where we execute the monitor.py script.
Explaining the script:
1. To get the open TCP/UDP ports:
We use the socket module.
- Ip = socket.gethostbyname (socket.gethostname())
The above line fetches the ip address of the server and saves the ip address in the variable ip.
Using socket bind() function with try and except block we can get the open TCP/UDP ports.
2. To get average delay between links:
We are going to use the os module here which uses the ping command along with timedate module.
In the code we get an average delay for 5 times with an interval of 1sec each.
If average delay exceeds the threshold value 0.25s then it will save the average delay in the log file with the logging:yes indication.
3. To get RAM, CPU and Disk usage:
We are going to use the module psutil to get system informations like cpu usage,memory usage,disk usage, network information etc..,
We are going to get the following informations :
hostname
Uuid
system
uptime
cpu_count
Cpu_usage
memory_total
memory_used
memory_used_percent
drives
network_up : network_stats["traffic_out"]
network_down : network_stats["traffic_in"]
network_cards
timestamp
Thank you!