Ad Widget

Collapse

RSS feeds for Zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bert ter Beest
    Junior Member
    • May 2008
    • 11

    #1

    RSS feeds for Zabbix

    Just installed a new Zabbix production server, and I was looking for integrated RSS support. Still cannot find that, but our company is using RSS feeds a lot on the old server. I thus moved my old RSS script to the new server. The script is not on this forum anymore it seems (at least not a version with minor security and the like). Therefore, for everyone who wants to use RSS feeds with their Zabbix installations, I put it here for the world to see

    I used some script, and added a login option to make it somewhat secure, just put the code below in a file (rss.php) and put that in a directory of its own. Make sure you change the $real_user and $real_pass variables to the login credentials you want for your feed.

    PHP Code:
    <?
    /**********************************************************************************
     Modifications by Bert ter Beest. Also added some security. Change login information on setup!
     For questions, please post on the Zabbix forum.
     **********************************************************************************/
    is_user_valid();

    function is_user_valid() {
         $auth=false;
         if (isset( $_SERVER['PHP_AUTH_USER'] ) && isset($_SERVER['PHP_AUTH_PW'])) { 
                $real_user = "rss_username"; // setup a username for the RSS feed here
                   $real_pass = "rss_password"; //setup a password for the RSS feed here
            if ($_SERVER['PHP_AUTH_PW'] == $real_pass and $_SERVER['PHP_AUTH_USER'] == $real_user) {
                   $auth = true;
               }
          } 
         if (!$auth) { 
              header( 'WWW-Authenticate: Basic realm="Infotune secure rss"' ); 
              header( 'HTTP/1.0 401 Unauthorized' ); 
              echo 'Authorization Required.'; 
              exit(); 
          } 
    }
    // set the file's content type and character set 
    // this must be called before any output
    header("Content-Type: text/xml;charset=utf-8");

    /**********************************************************************************
     * rss.php
     * Version: 1.00
     * Author: Rogers Cadenhead
     * Date: 05/21/2004
     * http://www.cadenhead.org/workbench/
     *
     **********************************************************************************
     This program is distributed in the hope that it will be useful, but WITHOUT ANY  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A  PARTICULAR PURPOSE.

     This work is hereby released into the Public Domain. To view a copy of the public  domain dedication, visit http://creativecommons.org/licenses/publicdomain/ or  send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California  94305, USA.
     **********************************************************************************/

    // prepare HTML text for use as UTF-8 character data in XML 
    function cleanText($intext) {
        return utf8_encode(
            htmlspecialchars(
                stripslashes($intext)));
    }



    // retrieve database records
    include('db_connect.php');
    // the file db_connect.php contains only one line:
    // $db = mysql_pconnect("localhost", "usernameHere", "passwordHere"); // it should be readable only by the user running the Web server // and saved outside of the directory tree that holds Web pages

    if (!$db)
    {
       error_log("Error: Could not connect to database in rss.php.");
       exit;
    }

    // store items from the database in the $result1 array mysql_select_db("zabbix");

    $query1 = "select triggers.description description, items.lastvalue lastvalue, hosts.host host from triggers, functions, items, hosts where triggers.triggerid = functions.triggerid and items.itemid = functions.itemid and hosts.hostid = items.hostid and hosts.host like '%' and hosts.status=0 and triggers.value = 1";

    $result1 = mysql_query($query1);
    $phpversion = phpversion();

    // display RSS 2.0 channel information

    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
    <rss version=\"2.0\">
       <channel>
          <title>Current Triggers that are active</title>
          <link>http://monitor.com.</link>
          <description>Triggers Status</description>
          <language>en-us</language>
          <docs>http://backend.userland.com/rss</docs>
          <generator>PHP/$phpversion</generator>";

    // loop through the array pulling database fields for each item 
    for ($i = 0; $i < mysql_num_rows($result1); $i++) {
       @$row = mysql_fetch_array($result1);
       $trigger = cleanText($row["description"]);
       $hostname = cleanText($row["host"]);
       $value = cleanText($row["lastvalue"]);
       $title = "Host: ".cleanText($row["host"])." Trigger: ".cleanText($row["description"])." Value: ".cleanText($row["lastvalue"]);

    // display an item
    echo "<item>
             <title>$title</title>
             <link>$trigger</link>
             <description>$trigger</description>
             <pubDate>$trigger</pubDate>
             <guid isPermaLink=\"false\">$trigger</guid>
          </item>";
    }

    echo "</channel></rss>";
    ?>
    Then make a file called db_connect.php in the same directory and make it look like this;

    PHP Code:
    <?php
    $db 
    mysql_pconnect("localhost""mysql_username""mysql_password"); 
    ?>
    Ofcourse change this file so that it has the database login information for the Zabbix MySQL database.

    Script is provided as-is, I take no resposibility for it whatsoever, mostly because I just copied and pasted from several scripts to get this working right (see the comments in the code for copyright information and such). I am however willing to support it if that is needed by any of you.
    Last edited by Bert ter Beest; 13-05-2011, 09:14. Reason: typo's and added clarifications
  • JBo
    Senior Member
    • Jan 2011
    • 310

    #2
    Hi,

    Originally posted by Bert ter Beest
    Just installed a new Zabbix production server, and I was looking for integrated RSS support. Still cannot find that, but our company is using RSS feeds a lot on the old server.
    If you'd asked before, I would have pointed you to my own script.
    It is probably a little bit harder to set up but has a few advantages:
    • it defines RSS as a new Zabbix «media».
    • it is possible to have several RSS feeds.
    • each user can have his own RSS feed.
    • it is easy to filter events sent to RSS feeds by origin or severity in scenarios.
    • RSS feeds are flat files, no access to Zabbix DB to serve them.

    There is no user authentication in the script. I define it as a «Basic HTTP Authentication» in Apache configuration.

    Sorry to hijack your thread with a shameless ad but it may be useful to somebody else.

    Regards,
    JBo

    Comment

    • Bert ter Beest
      Junior Member
      • May 2008
      • 11

      #3
      Those are very nice features, I will give it a looksee

      Apache Auth is not useful for me though, I need it integrated in the script, hence my own work...

      Anyway, I think both solutions can co-exist 'cause they handle stuff differently (why the flatfiles btw?). Also, this script does not require you to have root access, can be handy to some I guess.

      Lets use this as a rollup thread for RSS scripts maybe, that way everyone can find it without any hassle.

      I will post the final running version of my script in a few days when I am not so busy.

      Comment

      • JBo
        Senior Member
        • Jan 2011
        • 310

        #4
        Hi,

        Originally posted by Bert ter Beest
        why the flatfiles btw?
        It was the easiest way to implement custom RSS feeds without modifying Zabbix DB schema.
        Also, some people tend to set very aggressive refresh periods in their RSS reader (it is supposed to be real time monitoring alerts, not a post of new article on some blog). By serving them from flat files, there is no additional load on database.

        Regards,
        JBo

        Comment

        • starfish
          Junior Member
          • Nov 2011
          • 6

          #5
          actions setup

          hi,
          just tried to get JBo´s solution working, but I couldn´t sort out how to configure actions for continuous temperature data reading on the feed.
          Could someone please explain. thanks
          starfish

          Comment

          • JBo
            Senior Member
            • Jan 2011
            • 310

            #6
            Hi,

            Originally posted by starfish
            just tried to get JBo´s solution working, but I couldn´t sort out how to configure actions for continuous temperature data reading on the feed.
            You cannot directly send data to the feed.
            Zabbix actions are tied to triggers.
            If you want to get temperature data sent to RSS, the easiest way is probably to define a trigger based on temperature change.
            Something like:
            Code:
            {host:temp.change(0)}#0
            should be OK.

            Hope this helps,
            JBo

            Comment

            • starfish
              Junior Member
              • Nov 2011
              • 6

              #7
              thank you for the hint. I'll try it that way. Sometimes I am struggling with funny things like: I couldnt find the place where to insert triggers, until I found that in my german version it's called "Ausloeser"
              rgds
              starfish

              Comment

              Working...