NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import subprocess
import smtplib
from email.mime.text import MIMEText

threshold = 5

strr=""

def report_via_email(msgtext,subject):
msg = MIMEText(msgtext)
msg["Subject"] = subject
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.ehlo()
server.starttls()
server.login("batsam98","Batra@1996")
server.sendmail("[email protected]","[email protected]",msg.as_string())

def disk_space():
global strr
strr=""
df = subprocess.Popen(["df","-h"], stdout=subprocess.PIPE)
for line in df.stdout:
if line.decode().split()[5] == "/":
if int(line.decode().split()[4][:-1]) > threshold:
strr+=" The file system mounted on / is consuming more than threshold " + "n"
disk_usage()
#report_via_email("The file system mounted on / is consuming more than threshold","Low disk space warning")

def disk_usage():
global strr
df = subprocess.Popen(["du","-hx"], stdout=subprocess.PIPE)
df1 = subprocess.Popen(["sort","-n","-r"], stdin=df.stdout,stdout=subprocess.PIPE)
df2 = subprocess.Popen(["head","-n" "5"], stdin=df1.stdout,stdout=subprocess.PIPE)
for line in df2.stdout:
strr+=line.decode().split()[1] + " is consuming " + line.decode().split()[0] + "." + "n"
report_via_email(strr,"Disk space alert")



def memory_usage():
global strr
strr=""
df = subprocess.Popen(["free","-m"],stdout=subprocess.PIPE)
df1 = subprocess.Popen(["grep","Mem"],stdin=df.stdout,stdout=subprocess.PIPE)
for line in df1.stdout:
total=int(line.decode().split()[1])
used=int(line.decode().split()[2])
#print(total,used,total+used)
mem_usage=(used/total)*100
strr+=str(mem_usage) + " % memory has been consumed " + "." + "n"
#print(strr)
#report_via_email(strr,"Memory usage alert")
top_stats()



def uptime():
global strr
strr=""
df = subprocess.Popen(["uptime"],stdout=subprocess.PIPE)
#df1 = subprocess.Popen(["grep","Tasks"],stdin=df.stdout,stdout=subprocess.PIPE)
for line in df.stdout:
updurationhrs = line.decode().split()[2][:-1].split(":")[0]
updurationmin = line.decode().split()[2][:-1].split(":")[1]
load1=line.decode().split()[7][:-1]
load2=line.decode().split()[8][:-1]
load3=line.decode().split()[9][:-1]
load = load1 + " " + load2 + " " + load3 + " respectively " + "."
strr+="System is up for " + str(updurationhrs) + " and " + str(updurationmin) + " minutes " + " with load averages for last 1 ,5 and 15 minutes as " + load
#print(strr)
report_via_email(strr,"uptime and load average alert")


def top_stats():
temp=""
global strr
df = subprocess.Popen(["top","-o","%MEM","-n","1"],stdout=subprocess.PIPE)
df1 = subprocess.Popen(["grep","S|R"],stdin=df.stdout,stdout=subprocess.PIPE)
df2 = subprocess.Popen(["grep",":"],stdin=df1.stdout,stdout=subprocess.PIPE)
df3 = subprocess.Popen(["tail","-n","+2"],stdin=df2.stdout,stdout=subprocess.PIPE)
df4 = subprocess.Popen(["head","-n","1"],stdin=df3.stdout,stdout=subprocess.PIPE)
df5 = subprocess.Popen(["top","-o","%MEM","-n","1"],stdout=subprocess.PIPE)
df6 = subprocess.Popen(["grep","Tasks:"],stdin=df5.stdout,stdout=subprocess.PIPE)
for line in df6.stdout:
temp = line.decode().split()[7] + " stopped " + line.decode().split()[9] + " zombie "
strr+=line.decode().split()[1] + " Tasks " + line.decode().split()[3] + " running " + line.decode().split()[5] + " sleeping " + temp + "n"
for line in df4.stdout:
strr+=line.decode().split()[2] + " memory consumption is " + line.decode().split()[10] + " % " + "n"
#print(strr)
report_via_email(strr,"memory consumption alert")



def monitoring():
application_monitoring()
server_monitoring
http_logs()



def application_monitoring():
global strr
strr=""
ldate=""
ltime=""
outF = open("/opt/tomcat/logs/last_date_app.txt", "r")
content1 = outF.read()
outF.close
outF1 = open("/opt/tomcat/logs/last_time_app.txt", "r")
content2=outF1.read()
outF1.close()
df = subprocess.Popen(["cat","/opt/tomcat/logs/catalina.out"], stdout=subprocess.PIPE)
df1 = subprocess.Popen(["grep","ERROR"], stdin=df.stdout,stdout=subprocess.PIPE)
df2 = subprocess.Popen(["sort","-n","-r"], stdin=df1.stdout,stdout=subprocess.PIPE)
for line in df2.stdout:
if line.decode().split()[0] >= content1 and line.decode().split()[1] > content2:
if line.decode().split()[0] > ldate:
ldate=line.decode().split()[0]
if line.decode().split()[1] > ltime:
ltime=line.decode().split()[1]
strr+=line.decode().split(":")[3] + "n"
#print(line.decode().split(":")[3])
else:
break
outF = open("/opt/tomcat/logs/last_date_app.txt", "w")
outF.write(str(ldate))
outF1 = open("/opt/tomcat/logs/last_time_app.txt", "w")
outF1.write(str(ltime))
outF.close()
outF1.close()
#report_via_email(strr,"Application Error Alert")


def http_logs():
global strr
strr=""
#ldate=""
ltime=""
#outF = open("/opt/tomcat/logs/last_date_http.txt", "r")
#content1 = outF.read()
#outF.close
outF1 = open("/opt/tomcat/logs/last_time_http.txt", "r")
content2=outF1.read()
outF1.close()
df = subprocess.Popen(["cat","/opt/tomcat/logs/localhost_access_log.2019-08-03.txt"], stdout=subprocess.PIPE)
dfe1 = subprocess.Popen(["grep","404"], stdin=df.stdout,stdout=subprocess.PIPE)
dfe1sort = subprocess.Popen(["sort","-n","-r"],stdin=dfe1.stdout,stdout=subprocess.PIPE)
dfe2 = subprocess.Popen(["grep","400"], stdin=df.stdout,stdout=subprocess.PIPE)
dfe2sort = subprocess.Popen(["sort","-n","-r"],stdin=dfe1.stdout,stdout=subprocess.PIPE)
dfe3 = subprocess.Popen(["grep","401"], stdin=df.stdout,stdout=subprocess.PIPE)
dfe3sort = subprocess.Popen(["sort","-n","-r"],stdin=dfe1.stdout,stdout=subprocess.PIPE)
dfe4 = subprocess.Popen(["grep","403"], stdin=df.stdout,stdout=subprocess.PIPE)
dfe4sort = subprocess.Popen(["sort","-n","-r"],stdin=dfe1.stdout,stdout=subprocess.PIPE)
dfe5 = subprocess.Popen(["grep","500"], stdin=df.stdout,stdout=subprocess.PIPE)
dfe5sort = subprocess.Popen(["sort","-n","-r"],stdin=dfe1.stdout,stdout=subprocess.PIPE)
for line in dfe2sort.stdout:
if line.decode().split()[3][13:21] > content2:
if line.decode().split()[3][13:21] > ltime:
ltime=line.decode().split()[3][13:21]
strr+="400 http error because " + line.decode().split()[6]+ " was a bad request ." +"n"
#print("404 not found error because "+line.decode().split()[4][5:-11] + " could not be found ." + "n"
else:
break
for line in dfe1sort.stdout:
#print(line.decode().split()[3][13:21])
if line.decode().split()[3][13:21] > content2:
if line.decode().split()[3][13:21] > ltime:
ltime=line.decode().split()[3][13:21]
strr+="404 not found error because " + line.decode().split()[6]+ " could not be found ." +"n"
#print("404 not found error because "+line.decode().split()[6] + " could not be found ." + "n")
else:
break
for line in dfe3sort.stdout:
if line.decode().split()[3][13:21] > content2:
if line.decode().split()[3][13:21] > ltime:
ltime=line.decode().split()[3][13:21]
strr+="401 http error because " + line.decode().split()[6]+ " needs a username and password ." +"n"
else:
break
for line in dfe4sort.stdout:
if line.decode().split()[3][13:21] > content2:
if line.decode().split()[3][13:21] > ltime:
ltime=line.decode().split()[3][13:21]
strr+="403 http error because Access is forbidden to " + line.decode().split()[6] + "n"
else:
break
for line in dfe4sort.stdout:
if line.decode().split()[3][13:21] > content2:
if line.decode().split()[3][13:21] > ltime:
ltime=line.decode().split()[3][13:21]
strr+="Internal server error " + "n"
else:
break
#outF = open("/opt/tomcat/logs/last_date_http.txt","w")
#outF.write(str(ldate))
outF1 = open("/opt/tomcat/logs/last_time_http.txt", "w")
outF1.write(str(ltime))
#outF.close()
outF1.close()
#report_via_email(strr,"Http Error Alert")

def server_monitoring():
global strr
strr=""
ldate=""
ltime=""
outF = open("/opt/tomcat/logs/last_date.txt", "r")
content1=outF.read()
outF.close
outF1 = open("/opt/tomcat/logs/last_time.txt","r")
content2=outF1.read()
outF1.close()
df = subprocess.Popen(["cat","/opt/tomcat/logs/catalina.out"], stdout=subprocess.PIPE)
df1 = subprocess.Popen(["grep","SEVERE"], stdin=df.stdout,stdout=subprocess.PIPE)
df2 = subprocess.Popen(["sort","-n","-r"], stdin=df1.stdout,stdout=subprocess.PIPE)
for line in df2.stdout:
if line.decode().split()[1] > content2:
if line.decode().split()[0] > ldate:
ldate=line.decode().split()[0]
if line.decode().split()[1] > ltime:
ltime=line.decode().split()[1]
strr+=line.decode()
#print(line.decode())
else:
break
outF = open("/opt/tomcat/logs/last_date.txt", "w")
outF.write(str(ldate))
outF1 = open("/opt/tomcat/logs/last_time.txt", "w")
outF1.write(str(ltime))
outF.close()
outF1.close()
report_via_email(strr,"Server Error Alert")

#disk_space()
#disk_usage()
#server_monitoring()
#memory_usage()
#uptime()
#top_stats()
#http_logs()
application_monitoring()
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.