Skip to content

Instantly share code, notes, and snippets.

@stick
Created July 10, 2012 14:21
Show Gist options
  • Save stick/3083582 to your computer and use it in GitHub Desktop.
Save stick/3083582 to your computer and use it in GitHub Desktop.
server_alert
#!/usr/bin/env python
SENDMAIL = "/usr/sbin/sendmail"
import os
import sys
def main():
n_host_alias = os.getenv("NAGIOS_HOSTALIAS")
n_host_address = os.getenv("NAGIOS_HOSTADDRESS")
n_long_date = os.getenv("NAGIOS_LONGDATETIME")
n_service_description = os.getenv("NAGIOS_SERVICEDESC")
n_service_state = os.getenv("NAGIOS_SERVICESTATE")
n_service_output = os.getenv("NAGIOS_SERVICEOUTPUT")
n_service_check_command = os.environ['NAGIOS_SERVICECHECKCOMMAND']
n_service_notes_url = os.getenv("NAGIOS_SERVICENOTESURL")
n_notification_type = os.getenv("NAGIOS_NOTIFICATIONTYPE")
n_contact_email = os.getenv("NAGIOS_CONTACTEMAIL")
n_service_ack_author_alias = os.getenv("NAGIOS_SERVICEACKAUTHORALIAS")
n_service_ack_comment = os.getenv("NAGIOS_SERVICEACKCOMMENT")
logwatch_command = "check_mk-logwatch"
email = os.popen("%s -t" % SENDMAIL, "w")
email.write("To: " + n_contact_email + "\n")
email.write("Subject: ** " + n_notification_type + " Service Alert: " + n_host_alias + "/" + n_service_description + " is "+ n_service_state +" **\n")
email.write("\n")
email.write("***** Nagios / Check_MK *****\n\n")
email.write("Notification Type: " + n_notification_type + "\n\n")
if n_notification_type == "ACKNOWLEDGEMENT":
email.write("Comment: " + n_service_ack_comment + " -- " + n_service_ack_author_alias + "\n\n")
email.write("Service: " + n_service_description + "\n")
email.write("Host: " + n_host_alias + "\n")
email.write("Address: " + n_host_address + "\n")
email.write("State: " + n_service_state + "\n\n")
email.write("Date/Time: " + n_long_date + "\n\n")
email.write("Additional Info:\n\n" + n_service_output + "\n\n")
if n_service_check_command == logwatch_command:
logfile_name = n_service_description[4:]
if logfile_name.find('//'):
logfile_name = logfile_name.replace("/", "\\")
logfile = open("/var/lib/check_mk/logwatch/" + n_host_alias + "/" + logfile_name)
email.write("\n-- Logfile Output --\n")
for line in logfile:
if line.startswith('C'):
email.write(line)
email.write("\nView Logs: http://" + os.getenv("HOSTNAME") + n_service_notes_url)
email.write("\nAcknowledge: http://" + os.getenv("HOSTNAME") + n_service_notes_url + "&ack=1")
email.write("DEBUG logwatch_command: " + logwatch_command)
email.write("DEBUG n_service_description: " + n_service_description[4:])
status = email.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment