Advertisement
peasoup23

send-statusreport.py

Jul 8th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import ast
  4. import datetime
  5. import subprocess
  6.  
  7. # Abfrage: Host Status Down oder Unreachable
  8. command = 'lq "GET hosts\nColumns: state name last_hard_state_change plugin_output\nFilter: state = 1\nFilter: state = 2\nOr: 2\nOutputFormat: python"'
  9. hs = ast.literal_eval(subprocess.check_output(command, shell=True, text=True))
  10.  
  11. # Abfrage: Service Status Warn oder Critical
  12. command = 'lq "GET services\nColumns: state host_name description plugin_output last_hard_state_change\nFilter: state = 1\nFilter: state = 2\nOr: 2\nOutputFormat: python"'
  13. ss = ast.literal_eval(subprocess.check_output(command, shell=True, text=True))
  14.  
  15. body = ""
  16.  
  17. # Host Status
  18. #if len(hs) > 0:
  19. body = "==== Host Problems ====\n"
  20. for x in hs:
  21.         # excluded hosts 'host1' and 'host2'
  22.         if x[1] != "host1" and x[1] != "host2":
  23.                 if x[0] == 1:
  24.                         state = "DOWN"
  25.                 if x[0] == 2:
  26.                         state = "UNREACH"
  27.  
  28.                 ts = datetime.datetime.fromtimestamp(x[2]).strftime('%a %d.%m.%Y %H:%M:%S')
  29.  
  30.                 body = body + state + " | " + x[1] + " | " + ts
  31.                 body = body + "https://zh-hq-cmk01/mysite/check_mk/view.py?is_host_acknowledged=0&view_name=hostproblems" + "\n\n"
  32.  
  33. # Service Status
  34. #if len(ss) > 0:
  35. body = body + "\n==== Service Problems ====\n"
  36. for x in ss:
  37.         # excluded hosts 'host1' and 'host2'
  38.         if x[1] != "host1" and x[1] != "host2":
  39.                 if x[0] == 1:
  40.                         state = "WARN"
  41.                 if x[0] == 2:
  42.                         state = "CRIT"
  43.  
  44.                 ts = datetime.datetime.fromtimestamp(x[4]).strftime('%a %d.%m.%Y %H:%M:%S')
  45.  
  46.                 body = body + state + " | " + x[1] + " | " + x[2] + " | " + x[3]+ " | " + ts + "\n\n"
  47.  
  48. if len(hs) > 0 or len(ss) > 0:
  49.     body = body + "https://zh-hq-cmk01/mysite/check_mk/view.py?is_service_acknowledged=0&view_name=svcproblems"
  50.     print(body)
  51.  
  52.     import smtplib
  53.     from email.mime.text import MIMEText
  54.     from email.mime.multipart import MIMEMultipart
  55.  
  56.     # Set up the sender, recipient, and message content
  57.     sender = '[email protected]'
  58.     recipient = '[email protected]'
  59.     subject = 'checkmk status'
  60.     #body = 'This is a test email sent from Python!'
  61.  
  62.     # Create a multipart message
  63.     msg = MIMEMultipart()
  64.     msg['From'] = sender
  65.     msg['To'] = recipient
  66.     msg['Subject'] = subject
  67.  
  68.     # Add the message body as a plain text attachment
  69.     msg.attach(MIMEText(body, 'plain'))
  70.  
  71.     # Connect to the SMTP server (in this case, Gmail)
  72.     server = smtplib.SMTP('localhost', 25)
  73.     #server.starttls()
  74.  
  75.     # Send the email
  76.     server.sendmail(sender, recipient, msg.as_string())
  77.  
  78.     # Disconnect from the SMTP server
  79.     server.quit()
  80.  
  81.     print('Email sent successfully!')
  82.     print('Recipient: ' + recipient)
Tags: ChCeckMk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement