Advertisement
Guest User

nagios_mail.php

a guest
Oct 2nd, 2011
1,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 28.16 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Extended Nagios Notification Mail
  4.  *
  5.  * Version 0.9
  6.  *
  7.  * Install:
  8.  *
  9.  * replace the notifying definitions in the misccomands.cfg. Make sure that PHP
  10.  * is installed and the path to the PHP-binary is correct.
  11.  *
  12.  *
  13.  * # Example misccommands.cfg
  14.  * define command{
  15.  *       command_name    host-notify-by-email
  16.  *       command_line    /usr/bin/php -q /opt/nagios_mail.php
  17.  *       }
  18.  *
  19.  * define command{
  20.  *       command_name    notify-by-email
  21.  *       command_line    /usr/bin/php -q /opt/nagios_mail.php
  22.  *       }
  23.  *
  24.  *
  25.  * Configuration:
  26.  *
  27.  * configure your domain address below in this file.
  28.  *
  29.  *
  30.  * @author     Otto Berger <otto@bergerdata.de>
  31.  * @copyright  Copyright (c) 2009, Otto Berger
  32.  * @license    http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
  33.  * @version    $Id: nagios_mail.php 10 2009-10-28 10:37:46Z otto $
  34.  */
  35.  
  36.  
  37. // CONFIGURATION
  38.  
  39. /**
  40. * Nagios url
  41. *
  42. * url to your nagios webinterface. Used for command-links inside the mail.
  43. */
  44. $config['nagios_url']           = 'http://example.com/nagios';
  45.  
  46. /**
  47. * From-address of the notification mail
  48. *
  49. */
  50. $config['mail_from_address']    = 'monitoring@example.com';
  51.  
  52. /**
  53. * additional to mail-to-address
  54. *
  55. * this address is also used as reciepient while using the test-mode
  56. * from command-line (and not run from nagios)
  57. */
  58. $config['mail_add_to_address']  = '';
  59.  
  60. /**
  61. * mail-subject template (host notification)
  62. *
  63. * you can use all Nagios-vars here, see example
  64. */
  65. $config['mail_subject_host']    = '[M] Host %%NOTIFICATIONTYPE%% %%HOSTNAME%% is %%HOSTSTATE%% (%%NOTIFICATIONNUMBER%%)';
  66.  
  67. /**
  68. * mail-subject template (service notification)
  69. *
  70. * you can use all Nagios-vars here, see example
  71. */
  72. $config['mail_subject_service'] = '[M] Service %%NOTIFICATIONTYPE%% %%HOSTNAME%% %%SERVICEDESC%% is %%SERVICESTATE%% (%%NOTIFICATIONNUMBER%%)';
  73.  
  74. /**
  75. * additional mail-headers
  76. *
  77. * adds additional header-lines to the mail header. In this example
  78. * uncomment the following lines to send outlook high-priority mails.
  79. */
  80. $config['mail_add_headers'] = array(
  81.     //'X-Priority: 1 (Highest)',
  82.     //'X-MSMail-Priority: High',
  83.     //'Importance: High',
  84.     );
  85.  
  86. /**
  87. * debug mode
  88. *
  89. * enables the debug mode. All variables sent from nagios are printed at the end
  90. * of the mail. Attention: only visible in the text-only variant of the mail!
  91. */
  92. $config['debug'] = false;
  93.  
  94.  
  95.  
  96.  
  97. /**
  98. * Advanced configuration of mail bodies
  99. *
  100. * experts only ;)
  101. * customize the view of the nagios-variables.
  102. */
  103.  
  104. // HOST DETAILS
  105.  
  106. $config['groups'][] = array(
  107.     'name' => 'Host details',
  108.     'active' => true,
  109.     'branches' => array(
  110.         array(
  111.             'name'  => 'Host Informations',
  112.             'active' => true,
  113.             'data'  => array(
  114.                 array(
  115.                     'name'          => 'Hostname',
  116.                     'nagios_env'    => 'HOSTNAME',
  117.                     'required'      => true,
  118.                     ),
  119.                 array(
  120.                     'name'          => 'Alias',
  121.                     'nagios_env'    => 'HOSTALIAS',
  122.                     'required'      => false,
  123.                     ),
  124.                 array(
  125.                     'name'          => 'Address',
  126.                     'nagios_env'    => 'HOSTADDRESS',
  127.                     'required'      => true,
  128.                     'type'          => 'link',
  129.                     ),
  130.                 array(
  131.                     'name'          => 'Description',
  132.                     'nagios_env'    => 'HOSTNOTES',
  133.                     'required'      => false,
  134.                     ),
  135.                 array(
  136.                     'name'          => 'URL',
  137.                     'nagios_env'    => 'HOSTNOTESURL',
  138.                     'required'      => false,
  139.                     'type'          => 'link',
  140.                     ),
  141.                 ),
  142.             ),
  143.         array(
  144.             'name'  => 'Host State',
  145.             'active' => true,
  146.             'data'  => array(
  147.                 array(
  148.                     'name'          => 'State',
  149.                     'nagios_env'    => 'HOSTSTATE',
  150.                     'required'      => false,
  151.                     ),
  152.                 array(
  153.                     'name'          => 'State-Type',
  154.                     'nagios_env'    => 'HOSTSTATETYPE',
  155.                     'required'      => false,
  156.                     ),
  157.                 array(
  158.                     'name'          => 'Attempt',
  159.                     'nagios_env'    => 'HOSTATTEMPT',
  160.                     'required'      => false,
  161.                     ),
  162.                 array(
  163.                     'name'          => 'Duration',
  164.                     'nagios_env'    => 'HOSTDURATION',
  165.                     'required'      => false,
  166.                     ),
  167.                 array(
  168.                     'name'          => 'Downtime',
  169.                     'nagios_env'    => 'HOSTDOWNTIME',
  170.                     'required'      => false,
  171.                     ),
  172.                 ),
  173.             ),
  174.         array(
  175.             'name'  => 'Host Group',
  176.             'active' => true,
  177.             'data'  => array(
  178.                 array(
  179.                     'name'          => 'Group',
  180.                     'nagios_env'    => 'HOSTGROUPNAME',
  181.                     'required'      => false,
  182.                     ),
  183.                 array(
  184.                     'name'          => 'Group Alias',
  185.                     'nagios_env'    => 'HOSTGROUPALIAS',
  186.                     'required'      => false,
  187.                     ),
  188.                 array(
  189.                     'name'          => 'Group Alias',
  190.                     'nagios_env'    => 'HOSTGROUPNOTES',
  191.                     'required'      => false,
  192.                     ),
  193.                 array(
  194.                     'name'          => 'Group Alias',
  195.                     'nagios_env'    => 'HOSTGROUPNOTESURL',
  196.                     'required'      => false,
  197.                     'type'          => 'link',
  198.                     ),
  199.                 ),
  200.             ),
  201.         array(
  202.             'name'  => 'Host-Acknowledgement',
  203.             'active' => true,
  204.             'data'  => array(
  205.                 array(
  206.                     'name'          => 'Author',
  207.                     'nagios_env'    => 'HOSTACKAUTHOR',
  208.                     'required'      => false,
  209.                     ),
  210.                 array(
  211.                     'name'          => 'Comment',
  212.                     'nagios_env'    => 'HOSTACKCOMMENT',
  213.                     'required'      => false,
  214.                     ),
  215.                 ),
  216.             ),
  217.         array(
  218.             'name'  => 'Host State Data',
  219.             'active' => true,
  220.             'data'  => array(
  221.                 array(
  222.                     'name'          => 'Command',
  223.                     'nagios_env'    => 'HOSTCHECKCOMMAND',
  224.                     'required'      => false,
  225.                     ),
  226.                 array(
  227.                     'name'          => 'Type',
  228.                     'nagios_env'    => 'HOSTCHECKTYPE',
  229.                     'required'      => false,
  230.                     ),
  231.                 array(
  232.                     'name'          => 'Latency',
  233.                     'nagios_env'    => 'HOSTLATENCY',
  234.                     'required'      => false,
  235.                     ),
  236.                 array(
  237.                     'name'          => 'Percentage',
  238.                     'nagios_env'    => 'HOSTPERCENTAGE',
  239.                     'required'      => false,
  240.                     ),
  241.                 ),
  242.             ),
  243.         array(
  244.             'name'  => 'Host-Output',
  245.             'active' => true,
  246.             'data'  => array(
  247.                 array(
  248.                     'name'          => false,
  249.                     'nagios_env'    => 'HOSTOUTPUT',
  250.                     'required'      => false,
  251.                     ),
  252.                 ),
  253.             ),
  254.         array(
  255.             'name'  => 'Host Times',
  256.             'active' => true,
  257.             'data'  => array(
  258.                 array(
  259.                     'name'          => 'Last Check',
  260.                     'nagios_env'    => 'LASTHOSTCHECK',
  261.                     'required'      => false,
  262.                     'type'          => 'timestamp',
  263.                     ),
  264.                 array(
  265.                     'name'          => 'Last State-Change',
  266.                     'nagios_env'    => 'LASTHOSTSTATECHANGE',
  267.                     'required'      => false,
  268.                     'type'          => 'timestamp',
  269.                     ),
  270.                 array(
  271.                     'name'          => 'Last Up',
  272.                     'nagios_env'    => 'LASTHOSTUP',
  273.                     'required'      => false,
  274.                     'type'          => 'timestamp',
  275.                     ),
  276.                 array(
  277.                     'name'          => 'Last Down',
  278.                     'nagios_env'    => 'LASTHOSTDOWN',
  279.                     'required'      => false,
  280.                     'type'          => 'timestamp',
  281.                     ),
  282.                 array(
  283.                     'name'          => 'Last Unrechable',
  284.                     'nagios_env'    => 'LASTHOSTUNREACHABLE',
  285.                     'required'      => false,
  286.                     'type'          => 'timestamp',
  287.                     ),
  288.                 ),
  289.             )
  290.         )
  291.     );
  292.  
  293.  
  294. // SERVICE DETAILS
  295.  
  296. $config['groups'][] = array(
  297.     'name' => 'Service details',
  298.     'active' => true,
  299.     'branches' => array(
  300.         array(
  301.             'name'  => 'Servive details',
  302.             'active' => true,
  303.             'data'  => array(
  304.                 array(
  305.                     'name'          => 'Service',
  306.                     'nagios_env'    => 'SERVICEDESC',
  307.                     'required'      => false,
  308.                     ),
  309.                 array(
  310.                     'name'          => 'Command',
  311.                     'nagios_env'    => 'SERVICECHECKCOMMAND',
  312.                     'required'      => false,
  313.                     ),
  314.                 array(
  315.                     'name'          => 'Type',
  316.                     'nagios_env'    => 'SERVICECHECKTYPE',
  317.                     'required'      => false,
  318.                     ),
  319.                 array(
  320.                     'name'          => 'Latency',
  321.                     'nagios_env'    => 'SERVICELATENCY',
  322.                     'required'      => false,
  323.                     ),
  324.                 array(
  325.                     'name'          => 'Percentage',
  326.                     'nagios_env'    => 'SERVICEPERCENTCHANGE',
  327.                     'required'      => false,
  328.                     ),
  329.                 ),
  330.             ),
  331.         array(
  332.             'name'  => 'Service Group',
  333.             'active' => true,
  334.             'data'  => array(
  335.                 array(
  336.                     'name'          => 'Group',
  337.                     'nagios_env'    => 'SERVICEGROUPNAME',
  338.                     'required'      => false,
  339.                     ),
  340.                 array(
  341.                     'name'          => 'Group Alias',
  342.                     'nagios_env'    => 'SERVICEGROUPALIAS',
  343.                     'required'      => false,
  344.                     ),
  345.                 array(
  346.                     'name'          => 'Notes',
  347.                     'nagios_env'    => 'SERVICEGROUPNOTES',
  348.                     'required'      => false,
  349.                     ),
  350.                 array(
  351.                     'name'          => 'Notes',
  352.                     'nagios_env'    => 'SERVICEGROUPNOTESURL',
  353.                     'required'      => false,
  354.                     'type'          => 'link',
  355.                     ),
  356.                 ),
  357.             ),
  358.         array(
  359.             'name'  => 'Service-Acknowledgement',
  360.             'active' => true,
  361.             'data'  => array(
  362.                 array(
  363.                     'name'          => 'Author',
  364.                     'nagios_env'    => 'SERVICEACKAUTHOR',
  365.                     'required'      => false,
  366.                     ),
  367.                 array(
  368.                     'name'          => 'Comment',
  369.                     'nagios_env'    => 'SERVICEACKCOMMENT',
  370.                     'required'      => false,
  371.                     ),
  372.                 ),
  373.             ),
  374.         array(
  375.             'name'  => 'Service State',
  376.             'active' => true,
  377.             'data'  => array(
  378.                 array(
  379.                     'name'          => 'State',
  380.                     'nagios_env'    => 'SERVICESTATE',
  381.                     'required'      => false,
  382.                     ),
  383.                 array(
  384.                     'name'          => 'State-Type',
  385.                     'nagios_env'    => 'SERVICESTATETYPE',
  386.                     'required'      => false,
  387.                     ),
  388.                 array(
  389.                     'name'          => 'Attempt',
  390.                     'nagios_env'    => 'SERVICEATTEMPT',
  391.                     'required'      => false,
  392.                     ),
  393.                 array(
  394.                     'name'          => 'Duration',
  395.                     'nagios_env'    => 'SERVICEDURATION',
  396.                     'required'      => false,
  397.                     ),
  398.                 array(
  399.                     'name'          => 'Downtime',
  400.                     'nagios_env'    => 'SERVICEDOWNTIME',
  401.                     'required'      => false,
  402.                     ),
  403.                 ),
  404.             ),
  405.         array(
  406.             'name'  => 'Service-Output',
  407.             'active' => true,
  408.             'data'  => array(
  409.                 array(
  410.                     'name'          => false,
  411.                     'nagios_env'    => 'SERVICEOUTPUT',
  412.                     'required'      => false,
  413.                     ),
  414.                 ),
  415.             ),
  416.         array(
  417.             'name'  => 'Service Times',
  418.             'active' => true,
  419.             'data'  => array(
  420.                 array(
  421.                     'name'          => 'Last Check',
  422.                     'nagios_env'    => 'LASTSERVICECHECK',
  423.                     'required'      => false,
  424.                     'type'          => 'timestamp',
  425.                     ),
  426.                 array(
  427.                     'name'          => 'Last State-Change',
  428.                     'nagios_env'    => 'LASTSERVICESTATECHANGE',
  429.                     'required'      => false,
  430.                     'type'          => 'timestamp',
  431.                     ),
  432.                 array(
  433.                     'name'          => 'Last OK',
  434.                     'nagios_env'    => 'LASTSERVICEOK',
  435.                     'required'      => false,
  436.                     'type'          => 'timestamp',
  437.                     ),
  438.                 array(
  439.                     'name'          => 'Last Critical',
  440.                     'nagios_env'    => 'LASTSERVICECRITICAL',
  441.                     'required'      => false,
  442.                     'type'          => 'timestamp',
  443.                     ),
  444.                 array(
  445.                     'name'          => 'Last Warning',
  446.                     'nagios_env'    => 'LASTSERVICEWARNING',
  447.                     'required'      => false,
  448.                     'type'          => 'timestamp',
  449.                     ),
  450.                 array(
  451.                     'name'          => 'Last Unknown',
  452.                     'nagios_env'    => 'LASTSERVICEUNKNOWN',
  453.                     'required'      => false,
  454.                     'type'          => 'timestamp',
  455.                     ),
  456.                 ),
  457.             )
  458.         )
  459.     );
  460.  
  461.  
  462.  
  463. // CONTACT DETAILS
  464.  
  465. $config['groups'][] = array(
  466.     'name' => 'Contact details',
  467.     'active' => true,
  468.     'branches' => array(
  469.         array(
  470.             'name'  => 'Contact Info',
  471.             'active' => true,
  472.             'data'  => array(
  473.                 array(
  474.                     'name'          => 'Name',
  475.                     'nagios_env'    => 'CONTACTNAME',
  476.                     'required'      => false,
  477.                     ),
  478.                 array(
  479.                     'name'          => 'Alias',
  480.                     'nagios_env'    => 'CONTACTALIAS',
  481.                     'required'      => false,
  482.                     ),
  483.                 array(
  484.                     'name'          => 'eMail',
  485.                     'nagios_env'    => 'CONTACTEMAIL',
  486.                     'required'      => false,
  487.                     'type'          => 'mail',
  488.                     ),
  489.                 array(
  490.                     'name'          => 'Pager',
  491.                     'nagios_env'    => 'CONTACTPAGER',
  492.                     'required'      => false,
  493.                     ),
  494.                 ),
  495.             ),
  496.         array(
  497.             'name'  => 'Contactgroup Info',
  498.             'active' => true,
  499.             'data'  => array(
  500.                 array(
  501.                     'name'          => 'Name',
  502.                     'nagios_env'    => 'CONTACTGROUPNAME',
  503.                     'required'      => false,
  504.                     ),
  505.                 array(
  506.                     'name'          => 'Alias',
  507.                     'nagios_env'    => 'CONTACTGROUPALIAS',
  508.                     'required'      => false,
  509.                     ),
  510.                 array(
  511.                     'name'          => 'Members',
  512.                     'nagios_env'    => 'CONTACTGROUPMEMBERS',
  513.                     'required'      => false,
  514.                     ),
  515.                 ),
  516.             )
  517.         )
  518.     );
  519.  
  520.  
  521. // STATISTICS
  522.  
  523. $config['groups'][] = array(
  524.     'name' => 'Statistics',
  525.     'active' => true,
  526.     'branches' => array(
  527.         array(
  528.             'name'  => 'Hosts-Totals',
  529.             'active' => true,
  530.             'data'  => array(
  531.                 array(
  532.                     'name'          => 'Problems',
  533.                     'nagios_env'    => 'TOTALHOSTPROBLEMS',
  534.                     'required'      => false,
  535.                     ),
  536.                 array(
  537.                     'name'          => 'Problems Unhandled',
  538.                     'nagios_env'    => 'TOTALHOSTPROBLEMSUNHANDLED',
  539.                     'required'      => false,
  540.                     ),
  541.                 array(
  542.                     'name'          => 'Down',
  543.                     'nagios_env'    => 'TOTALHOSTSDOWN',
  544.                     'required'      => false,
  545.                     ),
  546.                 array(
  547.                     'name'          => 'Down Unhandled',
  548.                     'nagios_env'    => 'TOTALHOSTSDOWNUNHANDLED',
  549.                     'required'      => false,
  550.                     ),
  551.                 array(
  552.                     'name'          => 'Unreachable',
  553.                     'nagios_env'    => 'TOTALHOSTSUNREACHABLE',
  554.                     'required'      => false,
  555.                     ),
  556.                 array(
  557.                     'name'          => 'Unreachable Unhandled',
  558.                     'nagios_env'    => 'TOTALHOSTSUNREACHABLEUNHANDLED',
  559.                     'required'      => false,
  560.                     ),
  561.                 array(
  562.                     'name'          => 'Up',
  563.                     'nagios_env'    => 'TOTALHOSTSUP',
  564.                     'required'      => false,
  565.                     ),
  566.                 ),
  567.             ),
  568.         array(
  569.             'name'  => 'Services-Totals',
  570.             'active' => true,
  571.             'data'  => array(
  572.                 array(
  573.                     'name'          => 'Problems',
  574.                     'nagios_env'    => 'TOTALSERVICEPROBLEMS',
  575.                     'required'      => false,
  576.                     ),
  577.                 array(
  578.                     'name'          => 'Problems Unhandled',
  579.                     'nagios_env'    => 'TOTALSERVICEPROBLEMSUNHANDLED',
  580.                     'required'      => false,
  581.                     ),
  582.                 array(
  583.                     'name'          => 'Critical',
  584.                     'nagios_env'    => 'TOTALSERVICESCRITICAL',
  585.                     'required'      => false,
  586.                     ),
  587.                 array(
  588.                     'name'          => 'Critical Unhandled',
  589.                     'nagios_env'    => 'TOTALSERVICESCRITICALUNHANDLED',
  590.                     'required'      => false,
  591.                     ),
  592.                 array(
  593.                     'name'          => 'Unknown',
  594.                     'nagios_env'    => 'TOTALSERVICESUNKNOWN',
  595.                     'required'      => false,
  596.                     ),
  597.                 array(
  598.                     'name'          => 'Unknown Unhandled',
  599.                     'nagios_env'    => 'TOTALSERVICESUNKNOWNUNHANDLED',
  600.                     'required'      => false,
  601.                     ),
  602.                 array(
  603.                     'name'          => 'Warning',
  604.                     'nagios_env'    => 'TOTALSERVICESWARNING',
  605.                     'required'      => false,
  606.                     ),
  607.                 array(
  608.                     'name'          => 'Warning Unhandled',
  609.                     'nagios_env'    => 'TOTALSERVICESWARNINGUNHANDLED',
  610.                     'required'      => false,
  611.                     ),
  612.                 array(
  613.                     'name'          => 'OK',
  614.                     'nagios_env'    => 'TOTALSERVICESOK',
  615.                     'required'      => false,
  616.                     ),
  617.                 ),
  618.             )
  619.         )
  620.     );
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627. class Nagios_Mail {
  628.  
  629.     var $config = array();
  630.     var $nagios = array();
  631.     var $replace = array();
  632.     var $notification_type;
  633.     var $notification_color;
  634.  
  635.     function setConfig($config) {
  636.         $this->config = (array)$config;
  637.     }
  638.  
  639.     function __construct() {
  640.  
  641.         foreach ($_ENV as $key => $value) {
  642.  
  643.             if (strpos($key, 'NAGIOS_') !== false) {
  644.                 $this->nagios[substr($key, 7)] = $value;
  645.                 $this->replace['%%' . substr($key, 7) . '%%'] = $value;
  646.             }
  647.         }
  648.     }
  649.  
  650.     function build() {
  651.  
  652.         if (!empty($this->nagios['SERVICECHECKTYPE'])) {
  653.             $this->notification_type = 'SERVICE';
  654.  
  655.             if (!empty($this->nagios['SERVICEACKAUTHOR'])) {
  656.                 $this->notification_type = 'SERVICE_ACK';
  657.             }
  658.         } else {
  659.             $this->notification_type = 'HOST';
  660.  
  661.             if (!empty($this->nagios['HOSTACKAUTHOR'])) {
  662.                 $this->notification_type = 'HOST_ACK';
  663.             }
  664.         }
  665.  
  666.         $this->str_info = '';
  667.  
  668.         if (!count($this->nagios)) {
  669.  
  670.             // Test-Mode
  671.  
  672.             foreach ($this->config['groups'] as $group) {
  673.                 foreach ($group['branches'] as $branch) {
  674.                     foreach ($branch['data'] as $field) {
  675.  
  676.                         if (!isset($field['type'])) {
  677.                             $field['type'] = false;
  678.                         }
  679.  
  680.                         if ($field['type'] == 'timestamp') {
  681.                             $value = time();
  682.                         } else {
  683.                             $value = $field['nagios_env'];
  684.                         }
  685.                         $this->nagios[$field['nagios_env']] = (strlen($value) > 17) ? substr($value, 0, 17) . "..." : $value;
  686.                     }
  687.                 }
  688.             }
  689.             $this->str_info .= ' TESTMODE: ';
  690.             $this->nagios['CONTACTEMAIL'] = '';
  691.             $this->nagios['NOTIFICATIONTYPE'] = '';
  692.             $this->nagios['NOTIFICATIONNUMBER'] = '';
  693.         }
  694.  
  695.  
  696.         if (!empty($this->nagios['CONTACTEMAIL']) || $this->config['mail_add_to_address']) {
  697.  
  698.  
  699.             switch($this->notification_type) {
  700.                 case 'HOST' :
  701.                 case 'HOST_ACK' :
  702.  
  703.                     $this->str_info .= strtr($this->config['mail_subject_host'], $this->replace);
  704.  
  705.                     switch ($this->nagios['HOSTSTATE']) {
  706.                         case 'UP' :
  707.                             $this->notification_color = '#00CC33'; // green
  708.                         break;
  709.                         case 'DOWN' :
  710.                             $this->notification_color = '#FF0000'; // red
  711.                         break;
  712.                         case 'UNREACHABLE' :
  713.                             $this->notification_color = '#FF0000'; // red
  714.                         break;
  715.                         default :
  716.                             $this->notification_color = '#999999'; // grey
  717.                         break;
  718.                     }
  719.  
  720.                 break;
  721.                 case 'HOST_ACK' :
  722.                     $this->str_info .= 'ACK FROM ' . $this->nagios['HOSTACKAUTHOR'];
  723.                 break;
  724.                 case 'SERVICE' :
  725.                 case 'SERVICE_ACK' :
  726.  
  727.                     $this->str_info .= strtr($this->config['mail_subject_service'], $this->replace);
  728.  
  729.                     switch ($this->nagios['SERVICESTATE']) {
  730.                         case 'OK' :
  731.                             $this->notification_color = '#00CC33'; // green
  732.                         break;
  733.                         case 'WARNING' :
  734.                             $this->notification_color = '#FF6600'; // orange
  735.                         break;
  736.                         break;
  737.                         case 'CRITICAL' :
  738.                             $this->notification_color = '#FF0000'; // red
  739.                         break;
  740.                         case 'UNKNOWN' :
  741.                         default :
  742.                             $this->notification_color = '#999999'; // grey
  743.                         break;
  744.                     }
  745.  
  746.                 break;
  747.                 case 'SERVICE_ACK' :
  748.                     $this->str_info .= 'ACK FROM ' . $this->nagios['SERVICEACKAUTHOR'];
  749.                 break;
  750.             }
  751.  
  752.             $info_types = array(
  753.                 'ACKNOWLEDGEMENT',
  754.                 'FLAPPINGSTART',
  755.                 'FLAPPINGSTOP',
  756.                 'FLAPPINGDISABLED',
  757.                 'DOWNTIMESTART',
  758.                 'DOWNTIMEEND',
  759.                 'DOWNTIMECANCELLED',
  760.                 );
  761.  
  762.             if (in_array($this->nagios['NOTIFICATIONTYPE'], $info_types)) {
  763.                 $this->notification_color = 'blue';
  764.             }
  765.  
  766.             $boundary = '----------_' . md5('Nagios_Mail_' . microtime());
  767.  
  768.             $headers = array();
  769.             $headers[] = 'MIME-Version: 1.0';
  770.             $headers[] = 'From: ' . $this->config['mail_from_address'];
  771.  
  772.             foreach ($this->config['mail_add_headers'] as $header) {
  773.                 $headers[] = $header;
  774.             }
  775.  
  776.             $headers[] = "Content-Type: multipart/alternative;\n boundary=\"" . $boundary . "\"";
  777.  
  778.  
  779.  
  780.             $body = "\n--" . $boundary . "\nContent-Transfer-Encoding: 8bit\nContent-Type: text/plain; charset=ISO-8859-15\n\n";
  781.             $body .= $this->getBodyText();
  782.             $body .= "\n--" . $boundary . "\nContent-Transfer-Encoding: 8bit\nContent-Type: text/html; charset=ISO-8859-15\n\n";
  783.             $body .= $this->getBodyHTML();
  784.             $body .= "\n--" . $boundary . "--\n";
  785.  
  786.  
  787.             if ($this->nagios['CONTACTEMAIL'] && $this->config['mail_add_to_address']) {
  788.                 $this->nagios['CONTACTEMAIL'] .= ', ' . $this->config['mail_add_to_address'];
  789.             } elseif (empty($this->nagios['CONTACTEMAIL'])) {
  790.                 $this->nagios['CONTACTEMAIL'] = $this->config['mail_add_to_address'];
  791.             }
  792.  
  793.             mail($this->nagios['CONTACTEMAIL'], $this->str_info, $body, implode("\n", $headers));
  794.  
  795.         } else {
  796.  
  797.             die("\nCONTACTEMAIL env-var is empty (not run from Nagios?) or 'mail_add_to_address' not configured (Testmode)\n\n");
  798.         }
  799.     }
  800.  
  801.  
  802.     function getBodyText() {
  803.  
  804.         $output_text[] = $this->str_info;
  805.  
  806.         if (strpos($this->notification_type, 'HOST') !== false) {
  807.             $output_text[] = '*Output*: ' . $this->nagios['HOSTOUTPUT'];
  808.         } else {
  809.             $output_text[] = '*Output*: ' . $this->nagios['SERVICEOUTPUT'];
  810.         }
  811.  
  812.         $output_text[] = '';
  813.  
  814.         foreach ($this->config['groups'] as $group) {
  815.  
  816.             if (!$group['active'] || $group['active'] != $this->notification_type) {
  817.                 continue;
  818.             }
  819.  
  820.             $group_text = array();
  821.  
  822.             foreach ($group['branches'] as $branch) {
  823.  
  824.                 if (!$branch['active'] || $branch['active'] != $this->notification_type) {
  825.                     continue;
  826.                 }
  827.  
  828.                 $branch_active = false;
  829.  
  830.                 $max_chars = 0;
  831.                 foreach ($branch['data'] as $value) {
  832.                     if (strlen($value['name']) > $max_chars && (!empty($this->nagios[$value['nagios_env']]) || $value['required']) || (!empty($this->nagios[$value['nagios_env']]) && empty($value['name']))) {
  833.                         $max_chars = strlen($value['name']);
  834.                         $branch_active = true;
  835.                     }
  836.                 }
  837.  
  838.                 if ($branch_active) {
  839.  
  840.                     $group_text[] = '*' . $branch['name'] . '*';
  841.  
  842.                     foreach ($branch['data'] as $field) {
  843.  
  844.                         $field['value'] = trim($this->nagios[$field['nagios_env']]);
  845.  
  846.                         if (!empty($field['value']) || $field['required']) {
  847.  
  848.                             if (!isset($field['type'])) {
  849.                                 $field['type'] = false;
  850.                             }
  851.  
  852.                             switch($field['type']) {
  853.                                 case 'timestamp' :
  854.                                     $field['value'] = date('d.m.Y H:i', $field['value']);
  855.                                 break;
  856.  
  857.                             }
  858.  
  859.                             if ($field['name']) {
  860.                                 $group_text[] = sprintf('%-'. ($max_chars+1) . 's: %s', $field['name'], $field['value']);
  861.                             } else {
  862.                                 $group_text[] = $field['value'];
  863.                             }
  864.                         }
  865.                     }
  866.  
  867.                     $group_text[] = '';
  868.                 }
  869.             }
  870.  
  871.             if (count($group_text)) {
  872.                 $output_text[] = '*' . $group['name'] . '*';
  873.                 $output_text[] = '------------------------------------';
  874.                 $output_text[] = implode("\n", $group_text);
  875.             }
  876.         }
  877.  
  878.         if ($this->config['nagios_url']) {
  879.  
  880.             $output_text[] = '/Nagios/:';
  881.             $output_text[] = '<' . $this->config['nagios_url'] . '>';
  882.  
  883.             if ($this->notification_type == 'HOST' && $this->nagios['NOTIFICATIONTYPE'] == 'PROBLEM') {
  884.                 $output_text[] = '/Acknowledge this problem/:';
  885.                 $output_text[] = '<' . $this->config['nagios_url'] . '/cgi-bin/cmd.cgi?cmd_typ=33&host=' . $this->nagios['HOSTNAME'] . '>';
  886.             } elseif ($this->notification_type == 'SERVICE' && $this->nagios['NOTIFICATIONTYPE'] == 'PROBLEM') {
  887.                 $output_text[] = '/Acknowledge this problem/:';
  888.                 $output_text[] = '<' . $this->config['nagios_url'] . '/cgi-bin/cmd.cgi?cmd_typ=34&host=' . $this->nagios['HOSTNAME'] . '&service=' . $this->nagios['SERVICEDESC'] . '>';
  889.             }
  890.  
  891.             $output_text[] = '';
  892.         }
  893.  
  894.         if ($this->config['debug']) {
  895.             ksort($this->nagios);
  896.             ob_start();
  897.             var_dump($this->nagios);
  898.             $output_text[] = "*DEBUG-OUTPUT*";
  899.             $output_text[] = ob_get_clean();
  900.         }
  901.  
  902.         return implode("\n", $output_text);
  903.  
  904.     }
  905.  
  906.  
  907.  
  908.     function getBodyHTML() {
  909.  
  910.         $output_html[] = <<< END
  911.  
  912.         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  913.         <html xmlns="http://www.w3.org/1999/xhtml">
  914.         <head>
  915.         <title></title>
  916.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
  917.         <meta name="viewport" content="width=600" />
  918.         </head>
  919.  
  920.         <body style="font-family: 'Courier New', Courier, monospace; font-size: 8pt;">
  921.  
  922. END;
  923.  
  924.         $output_html[] = '<table cellpadding="0" cellspacing="0" width="100%" style="font-family: \'Courier New\', Courier, monospace; font-size: 8pt;">';
  925.         $output_html[] = '<tr>';
  926.         $output_html[] = '<td width="16"><div style="height:16px;width:16px;background-color:' . $this->notification_color . ';">&nbsp;</div></td>';
  927.         $output_html[] = '<td style="font-size:15pt;font-weight:bold;color:#666666;padding-left:4px">Nagios Monitoring Message</td>';
  928.         $output_html[] = '</tr>';
  929.         $output_html[] = '</table>';
  930.  
  931.         $output_html[] = '<div style="background-color:#CCC;padding:5px;clear:both;">';
  932.         $output_html[] = '<div style="font-weight:bold;">';
  933.         $output_html[] = $this->str_info;
  934.         $output_html[] = '</div>';
  935.         $output_html[] = '<div style="margin-bottom:10px;">';
  936.  
  937.         if (strpos($this->notification_type, 'HOST') !== false) {
  938.             $output_html[] = '<strong>Output:</strong> ' . $this->nagios['HOSTOUTPUT'];
  939.         } else {
  940.             $output_html[] = '<strong>Output:</strong> ' . $this->nagios['SERVICEOUTPUT'];
  941.         }
  942.  
  943.         $output_html[] = '</div>';
  944.  
  945.         if ($this->config['nagios_url']) {
  946.             $output_html[] = '<div>';
  947.  
  948.             $output_html[] = '<a href="' . $this->config['nagios_url'] . '">' . $this->config['nagios_url'] . '</a>';
  949.  
  950.             if ($this->notification_type == 'HOST' && $this->nagios['NOTIFICATIONTYPE'] == 'PROBLEM') {
  951.                 $output_html[] = ' &#124; <a href="' . $this->config['nagios_url'] . '/cgi-bin/cmd.cgi?cmd_typ=33&host=' . $this->nagios['HOSTNAME'] . '">Acknowledge this problem</a>';
  952.             } elseif ($this->notification_type == 'SERVICE' && $this->nagios['NOTIFICATIONTYPE'] == 'PROBLEM') {
  953.                 $output_html[] = ' &#124; <a href="' . $this->config['nagios_url'] . '/cgi-bin/cmd.cgi?cmd_typ=34&host=' . $this->nagios['HOSTNAME'] . '&service=' . $this->nagios['SERVICEDESC'] . '">Acknowledge this problem</a>';
  954.             }
  955.  
  956.             $output_html[] = '</div>';
  957.         }
  958.  
  959.         $output_html[] = '</div>';
  960.  
  961.         foreach ($this->config['groups'] as $group) {
  962.  
  963.             if (!$group['active'] || $group['active'] != $this->notification_type) {
  964.                 continue;
  965.             }
  966.  
  967.             $group_html = array();
  968.  
  969.             foreach ($group['branches'] as $branch) {
  970.  
  971.  
  972.  
  973.                 if (!$branch['active'] || $branch['active'] != $this->notification_type) {
  974.                     continue;
  975.                 }
  976.  
  977.                 $branch_active = false;
  978.  
  979.                 $max_chars = 0;
  980.                 foreach ($branch['data'] as $value) {
  981.                     if (strlen($value['name']) > $max_chars && (!empty($this->nagios[$value['nagios_env']]) || $value['required']) || (!empty($this->nagios[$value['nagios_env']]) && empty($value['name']))) {
  982.                         $max_chars = strlen($value['name']);
  983.                         $branch_active = true;
  984.                     }
  985.                 }
  986.  
  987.                 $branch_html = array();
  988.  
  989.                 if ($branch_active) {
  990.  
  991.                     $branch_html[] = '<table cellspacing="0" cellpadding="0" width="280" style="border:1px solid #CFCFCF; font-family: \'Courier New\', Courier, monospace; font-size: 8pt;">';
  992.                     $branch_html[] = '<thead style="font-weight:bold; color:#003399; background-color:#CFCFCF;"><tr><td colspan="2">' . $branch['name'] . '</td></tr></thead>';
  993.                     $branch_html[] = '<tbody>';
  994.  
  995.                     foreach ($branch['data'] as $field) {
  996.  
  997.                         $field['value'] = trim($this->nagios[$field['nagios_env']]);
  998.  
  999.                         if (!empty($field['value']) || $field['required']) {
  1000.  
  1001.                             if (!isset($field['type'])) {
  1002.                                 $field['type'] = false;
  1003.                             }
  1004.  
  1005.                             switch($field['type']) {
  1006.                                 case 'timestamp' :
  1007.                                     $field['value'] = date('d.m.Y H:i', $field['value']);
  1008.                                 break;
  1009.                                 case 'link' :
  1010.  
  1011.                                     if (strpos($field['value'], 'http://') === false) {
  1012.                                         $field['value'] = sprintf('<a href="http://%s">%s</a>', $field['value'], $field['value']);
  1013.                                     } else {
  1014.                                         $field['value'] = sprintf('<a href="%s">%s</a>', $field['value'], $field['value']);
  1015.                                     }
  1016.  
  1017.                                 break;
  1018.                                 case 'mail' :
  1019.                                     $field['value'] = sprintf('<a href="mailto:%s">%s</a>', $field['value'], $field['value']);
  1020.                                 break;
  1021.  
  1022.                             }
  1023.  
  1024.                             if ($field['name']) {
  1025.                                 $branch_html[] = sprintf('<tr><td style="padding:1px 2px 1px 2px;width:120px; font-weight:bold;">%s</td><td>%s</td></tr>', $field['name'], $field['value']);
  1026.                             } else {
  1027.                                 $branch_html[] = sprintf('<tr><td style="padding:1px 2px 1px 2px;" colspan="2">%s</td></tr>', $field['value']);
  1028.                             }
  1029.                         }
  1030.                     }
  1031.  
  1032.                     $branch_html[] = '</tbody>';
  1033.                     $branch_html[] = '</table>';
  1034.  
  1035.                     $branch_html[] = '';
  1036.                 }
  1037.  
  1038.                 $group_html[] = implode("\n", $branch_html);
  1039.  
  1040.             }
  1041.  
  1042.             if (count($group_html)) {
  1043.  
  1044.                 $output_html[] = '<h2 style="font-size:10pt;font-weight:bold;color:#666666;border-bottom:1px solid #CCCCCC;clear:both;margin-top:15px;">' . $group['name'] . '</h2>';
  1045.  
  1046.                 $output_html[] = '<table width="100%" cellpadding="0" cellspacing="0">';
  1047.  
  1048.                 if (count($group_html) % 2 !== 0) {
  1049.                     $group_html[] = "";
  1050.                 }
  1051.  
  1052.                 foreach ($group_html as $i => $html) {
  1053.  
  1054.                     if ($i % 2 == 0) {
  1055.  
  1056.                         if ($i != 0){
  1057.                             $output_html[] = '</tr>';
  1058.                         }
  1059.  
  1060.                         $output_html[] = '<tr>';
  1061.                     }
  1062.  
  1063.                     $output_html[] = '<td ' . (($i % 2 == 0) ? 'width="285" ' : '') . 'valign="top" align="left" style="padding: 0 0 5px 0">' . $html . '</td>';
  1064.                 }
  1065.                 $output_html[] = '</tr>';
  1066.                 $output_html[] = '</table>';
  1067.             }
  1068.         }
  1069.  
  1070.         $output_html[] = '</body></html>';
  1071.  
  1072.         return implode("\n", $output_html);
  1073.     }
  1074. }
  1075.  
  1076.  
  1077. $nagios = new Nagios_Mail();
  1078. $nagios->setConfig($config);
  1079. $nagios->build();
  1080.  
  1081.  
  1082. ?>
  1083.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement