Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import ast
- import datetime
- import subprocess
- # Abfrage: Host Status Down oder Unreachable
- command = 'lq "GET hosts\nColumns: state name last_hard_state_change plugin_output\nFilter: state = 1\nFilter: state = 2\nOr: 2\nOutputFormat: python"'
- hs = ast.literal_eval(subprocess.check_output(command, shell=True, text=True))
- # Abfrage: Service Status Warn oder Critical
- 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"'
- ss = ast.literal_eval(subprocess.check_output(command, shell=True, text=True))
- body = ""
- # Host Status
- #if len(hs) > 0:
- body = "==== Host Problems ====\n"
- for x in hs:
- # excluded hosts 'host1' and 'host2'
- if x[1] != "host1" and x[1] != "host2":
- if x[0] == 1:
- state = "DOWN"
- if x[0] == 2:
- state = "UNREACH"
- ts = datetime.datetime.fromtimestamp(x[2]).strftime('%a %d.%m.%Y %H:%M:%S')
- body = body + state + " | " + x[1] + " | " + ts
- body = body + "https://zh-hq-cmk01/mysite/check_mk/view.py?is_host_acknowledged=0&view_name=hostproblems" + "\n\n"
- # Service Status
- #if len(ss) > 0:
- body = body + "\n==== Service Problems ====\n"
- for x in ss:
- # excluded hosts 'host1' and 'host2'
- if x[1] != "host1" and x[1] != "host2":
- if x[0] == 1:
- state = "WARN"
- if x[0] == 2:
- state = "CRIT"
- ts = datetime.datetime.fromtimestamp(x[4]).strftime('%a %d.%m.%Y %H:%M:%S')
- body = body + state + " | " + x[1] + " | " + x[2] + " | " + x[3]+ " | " + ts + "\n\n"
- if len(hs) > 0 or len(ss) > 0:
- body = body + "https://zh-hq-cmk01/mysite/check_mk/view.py?is_service_acknowledged=0&view_name=svcproblems"
- print(body)
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- # Set up the sender, recipient, and message content
- sender = '[email protected]'
- recipient = '[email protected]'
- subject = 'checkmk status'
- #body = 'This is a test email sent from Python!'
- # Create a multipart message
- msg = MIMEMultipart()
- msg['From'] = sender
- msg['To'] = recipient
- msg['Subject'] = subject
- # Add the message body as a plain text attachment
- msg.attach(MIMEText(body, 'plain'))
- # Connect to the SMTP server (in this case, Gmail)
- server = smtplib.SMTP('localhost', 25)
- #server.starttls()
- # Send the email
- server.sendmail(sender, recipient, msg.as_string())
- # Disconnect from the SMTP server
- server.quit()
- print('Email sent successfully!')
- print('Recipient: ' + recipient)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement