Advertisement
Guest User

Untitled

a guest
Aug 29th, 2022
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. import http.client
  2. import urllib
  3. import subprocess
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. import smtplib
  7. import http.client, urllib
  8. import urllib.request
  9.  
  10.  
  11. def main():
  12.     output = subprocess.run(["/bin/seaf-cli", "status"], capture_output=True)
  13.     print (output.stdout)
  14.  
  15.  
  16.     if "synchronized" in str(output.stdout):
  17.         crashed = 0
  18.     else:
  19.         crashed = 1
  20.  
  21.     if "error" in str(output.stdout):
  22.         error = 1
  23.     else:
  24.         error = 0
  25.  
  26.     if crashed == 1:
  27.         print("Crashed")
  28.         send_notification("Seafile_CLI-Sync possibly crashed")
  29.  
  30.     if crashed == 0 and error == 0:
  31.         send_notification("Doing nothing")
  32.  
  33.     if crashed == 0 and error == 1:
  34.         print("Send error message")
  35.         send_notification("Seafile-cli-sync shows error")
  36.  
  37.  
  38.  
  39.  
  40. def send_notification(message):
  41.     send_email(message)
  42.     pushover(message)
  43.     telegram_notification(message)
  44.  
  45.  
  46. def pushover(subject):
  47.     conn = http.client.HTTPSConnection("api.pushover.net:443")
  48.     conn.request("POST", "/1/messages.json",
  49.                  urllib.parse.urlencode({
  50.                      "token": "xxxxxxxxxxxxxxxxxxxx",
  51.                      "user": "xxxxxxxxxxxxxxxxxx",
  52.                      "message": subject,
  53.                  }), {"Content-type": "application/x-www-form-urlencoded"})
  54.     conn.getresponse()
  55.  
  56. def telegram_notification(subject):
  57.     conn = http.client.HTTPSConnection("api.telegram.org:443")
  58.     conn.request("POST", "/botxxxxxxxxxx:xxxxxxxxxxxxx/sendMessage?",
  59.                  urllib.parse.urlencode({
  60.                      'chat_id': 000000000000000,
  61.                      'text': subject,
  62.                  }), {"Content-type": "application/x-www-form-urlencoded"})
  63.     conn.getresponse()
  64.  
  65. def send_email(subject, message='No message provided'):
  66.     msg = MIMEMultipart()
  67.     password = "xxxxxxxxxxxxxx"
  68.     msg['From'] = "[email protected]"
  69.     msg['To'] = "[email protected]"
  70.     msg['Subject'] = subject
  71.     msg.attach(MIMEText(message, 'plain'))
  72.  
  73.     server = smtplib.SMTP('smtp.xxxxxx.xx: 587')
  74.     server.starttls()
  75.     server.login(msg['From'], password)
  76.     server.sendmail(msg['From'], msg['To'], msg.as_string())
  77.     server.quit()
  78.  
  79.  
  80. if __name__ == "__main__":
  81.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement