Skip to content

Instantly share code, notes, and snippets.

@devops-rob
Last active April 28, 2024 09:24
Show Gist options
  • Save devops-rob/c1e511090385871441c4d453c94a2737 to your computer and use it in GitHub Desktop.
Save devops-rob/c1e511090385871441c4d453c94a2737 to your computer and use it in GitHub Desktop.
Python script to monitor server resources, alerting in slack
#!/usr/bin/python
'''
Server Monitoring script to alert in slack by by DevOpsRob
Download the slacker python module by running wget https://pypi.python.org/packages/42/f9/3f3bcbe13b8c3aa4a134136cbbaa94beb1c5781f5a33b9317b45c699d453/slacker-0.9.60.tar.gz
Untar the file by running tar -xzvf slacker-0.9.60.tar.gz
Install the psutil module by yum install python-psutil -y
Enter the alert thresholds you would like to set in the variables section
For each alert, enter the slack channel name that you would like to notify in place of <slack-channel-name>
Install a crontab for this script to run at whatever poll interval you require for your monitoring. e.g. * * * * * /usr/bin/python /root/monitoring/slack_alerting.py
This script will also create a log file (/var/log/monitor.log) if it doesn't already exist and write the output of the server checks to it.
I highly recommend installing and configuring logrotate on this file
'''
#Import python libraries
import psutil
import os
from slacker import Slacker
import datetime
import time
#Variables
cpu_threshold=<insert-cpu-threshold-%>
mem_threshold=<insert-memory-threshold-%>
disk_threshold=<insert-diskspace-threshold-%>
slack = Slacker('<insert-your-slack-api-token-here>')
logfile='/var/log/monitor.log'
#CPU Alerting
if psutil.cpu_percent() > cpu_threshold:
slack.chat.post_message('#<insert-slack-channel-name-here>', 'CPU ALERT - CPU usage is currently above %s percent' % cpu_threshold)
print (time.strftime("%c") + ' - CPU ALERT - CPU usage is currently above %s percent' % cpu_threshold)
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - CPU ALERT - CPU usage is currently above %s percent' % cpu_threshold)
else:
print (time.strftime("%c") + ' - cpu is below %s percent' % cpu_threshold)
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - cpu is below %s percent' % cpu_threshold)
#Memory Alerting
if psutil.virtual_memory().percent > mem_threshold:
slack.chat.post_message('#<insert-slack-channel-name-here>', 'MEMORY ALERT - Memory usage is currently above %s percent' % mem_threshold)
print(time.strftime("%c") + ' - MEMORY ALERT - Memory usage is currently above %s percent' % mem_threshold)
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - MEMORY ALERT - Memory usage is currently above %s percent' % mem_threshold)
else:
print(time.strftime("%c") + ' - Memory usage is below %s percent' % mem_threshold)
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - Memory usage is below %s percent' % mem_threshold)
#Disk Usuage Alerting
if psutil.disk_usage('/').percent > disk_threshold:
slack.chat.post_message('#<insert-slack-channel-name-here>', 'DISK SPACE ALERT - Disk space usage is currently above %s percent' % disk_threshold)
print(time.strftime("%c") + ' - DISK SPACE ALERT - Disk space usage is currently above %s percent' % disk_threshold)
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - DISK SPACE ALERT - Disk space usage is currently above %s percent' % disk_threshold)
else:
print(time.strftime("%c") + ' - disk space usage is below %s percent' % disk_threshold)
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - disk space usage is below %s percent' % disk_threshold)
#Network Packet loss alerting
if psutil.net_io_counters().dropout > 0:
slack.chat.post_message('#<insert-slack-channel-name-here>', 'PACKET LOSS ALERT - The system is currently experiencing packet loss')
print(time.strftime("%c") + ' - PACKET LOSS ALERT - The system is currently experiencing packet loss')
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - PACKET LOSS ALERT - The system is currently experiencing packet loss')
else:
print(time.strftime("%c") + ' - No packets have been lost')
with open(logfile, 'a') as file_handle:
file_handle.write(time.strftime("%c") + ' - No packets have been lost')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment