Blogs on System Administration, Scripting, System Monitoring, Cloud Computing & Virtualizaton, VoIP Systems, SMS Systems and Web Applications Development.

OTRS Integration with Nagios


Introduction

This article is a complete guide to integrate Nagios Alerts with OTRS 5. 

Description

I have gone through many tutorials for Integration of OTRS with Nagios but either it needs a very complex configurations or you have to purchase licensed version of OTRS.
Following are the goals that were required to be achieved for OTRS integration with Nagios:

  1. All Nagios alerts will be sent to OTRS.
  2. There should be no auto-responses to Nagios Alerts.
  3. All Nagios alerts of same service and host must be attached in the same ticket till recovery.
  4. After recovery of the service, the ticket must be closed automatically.
Lets focus on each goal separately and discuss How it can be achieved in free versions of Nagios and OTRS?

Goal # 1 (All Nagios alerts will be sent to OTRS)

This is fairly simple, just point your OTRS integrated email i.e. support@example.com in your Nagios contacts.
$ sudo vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
        contact_name nagiosadmin        ; Short name of user
    use          generic-contact        ; Inherit default values from generic-contact template (defined above)
        alias        Nagios Admin       ; Full name of user
        email        support@example.com    ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }
Please note that I am using following Nagios addon for Nagios Alerts which gives very user friendly and colorful view of Nagios alerts and can be added in Nagios in few minutes i.e. Nagios Exchange Addon.

Goal # 2 (There should be no auto-responses to Nagios Alerts)

For disabling auto-responses to Nagios Alerts, you can configure it in OTRS Admin Panel.
Admin Tab -> System Administration -> SysConfig -> Ticket -> Core::PostMaster
Scroll Down to Option i.e. SendNoAutoResponseRegExp and add regex for Nagios i.e. PROBLEM|RECOVERY|nagios etc.


Goal # 3 (All Nagios alerts of same service and host must be attached in the same ticket till recovery)

This goal was little tricky in a sense that it is fact that you want to add follow up email on specific Ticket then OTRS need the Ticket number in the subject of the follow up email. So, I took benefit of this functionality and develop a PHP based API to handle it and placed on the server where the database of OTRS is hosted.
The API gets the service name as a parameter and returns the ticket number which is on either open or new state and have same service and host in its title.

Following is the simple code for the PHP API:
if(isset($_GET['service']) && $_GET['service'] != '' || isset($_GET['ticket']) && $_GET['ticket'] != '') {
  $dbhandle = mysql_connect('<db-host>', '<db-user>', '<db-password>') or die('DB Error: ' .mysql_error());    
  mysql_select_db('otrs',$dbhandle) or die('DB Error: Could not select database otrs');
}
if(isset($_GET['service']) && $_GET['service'] != '') {
  $query = "SELECT tn FROM `ticket` where ticket_state_id IN (1,4) and title like '%".urldecode($_GET['service'])."%' order by id desc limit 1;";
  $result = mysql_query($query) or die('Query Error: ' . mysql_error());
  $result = mysql_fetch_assoc($result);
  if(isset($result['tn'])) {
    echo $result['tn'];
  }
  else {
    echo 0;
  }
  mysql_close($dbhandle);
}
else {
    echo 0;
}

Now add this API in Nagios Emails Scripts:
$ sudo vi /usr/local/nagios/libexec/nagios_service_mail

Find the line 66 in script:
$subject = "$f_notify_type Service:$f_host_name/$f_serv_desc [$f_serv_state]";

Replace above line with following lines:
 $tn = file_get_contents("http://helpdesk.example.com/api.php?service=$f_host_name/$f_serv_desc");
 $subject = ($tn != '0' ? "Re: [Ticket#$tn] " : '')."$f_notify_type Service:$f_host_name/$f_serv_desc [$f_serv_state]";

Goal # 4 (After recovery of the service, the ticket must be closed automatically)

In order to achieve this goal, we need a trigger that can automatically close the ticket when service is restored. This trigger can be handled in OTRS with GenericAgent.
Navigate to Admin -> System Administration -> GenericAgent -> Add Job
Enter the name of the job, Automatic Execution Time and Even based Execution:
Fill only two fields in Select Tickets section as following:



Now again fill only two fields in Update Ticket section as following:


Now submit the changes.
That’s it. Enjoy Successful OTRS integration with Nagios.

0 comments:

Post a Comment

Please Enter your Comments Here.