Skip to content

Instantly share code, notes, and snippets.

@iann0036
Last active Sep 17, 2016
Embed
What would you like to do?
import smtplib
import os
import syslog
import requests
import subprocess
from email.mime.text import MIMEText
def alarmLocalHTTPPing():
millis = requests.get("http://127.0.0.1").elapsed.microseconds # /1000 ?
if millis > 100:
return "localhost responded in " + str(millis) + " milliseconds"
return False
def alarmRootDiskSpace():
percent_threshold = 90
stats = os.statvfs("/")
percent_free = stats.f_bavail * 100 / stats.f_blocks
if percent_free<(100-percent_threshold):
return 'Percentage of root disk free: ' + percent_free + '%'
return False
def alarmCurrentSessions():
raw_output = subprocess.check_output("/usr/bin/w")
lines = raw_output.splitlines()
if (len(lines)<3):
return False
message = "The following TTY sessions are open:\n"
for i in range(2,len(lines)):
split_parts = lines[i].split()
name = split_parts[0]
tty = split_parts[1]
from_host = split_parts[2]
logintime = split_parts[3]
message = message + "User: " + name + ", TTY: " + tty + ", Login Location: " + from_host + ", Time of Login: " + logintime
return message
def raiseEmailAlarm(message):
msg = MIMEText('Alarm Contents: ' + message)
from_addr = 'system@server.com'
to_addr = 'contact@ian.mn'
msg['Subject'] = '[ALERT] Alert generated from secureserver'
msg['From'] = from_addr
msg['To'] = to_addr
s = smtplib.SMTP('localhost')
s.sendmail(from_addr, [to_addr], msg.as_string())
s.quit()
def raiseHTTPAlarm(message):
postdata = {'message': message, 'user': 'contact@ian.mn'}
#requests.get("https://supersecurealert.com/api/", params=postdata)
def processAlarms():
alarms = list()
alarms.append(alarmLocalHTTPPing())
alarms.append(alarmCurrentSessions())
alarms.append(alarmRootDiskSpace())
for alarm in alarms:
if alarm != False:
syslog.syslog(alarm)
raiseEmailAlarm(alarm)
# OR #
raiseHTTPAlarm(alarm)
processAlarms()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment