Imported Upstream version 2.7
[ossec-hids.git] / contrib / ossec2rss.php
1 <?php
2 /* OSSEC 2 RSS script.
3  * by Daniel B. Cid ( dcid @ ossec.net)
4  *
5  * Just upload it to any web-accessible directory, and make
6  * sure the web server can access the OSSEC alerts log file.
7  */
8
9
10 $ossec_log = "/var/ossec/logs/alerts/alerts.log";
11 if(!is_readable($ossec_log))
12 {
13     echo "ERROR: Unable to access $ossec_log\n";
14     echo "*TIP: Make sure your web server can access that file. \n";
15     exit(1);
16 }
17
18 $timelp = filemtime($ossec_log);
19 $fh = fopen($ossec_log, "r");
20 if(!$fh)
21 {
22     exit(1);
23 }
24
25 if(filesize($ossec_log) > 30000)
26 {
27     fseek($fh, -30000, SEEK_END);
28     $line = fgets($fh, 4096);
29 }
30
31
32 $lastlines = array();
33 $event = array();
34 while($line = fgets($fh, 4096))
35 {
36     $line = trim($line);
37     if($line == "")
38     {
39         continue;
40     }
41
42     if(strncmp($line, "** Alert ", 9) == 0)
43     {
44         if(strncmp($event, "** Alert ", 9) == 0)
45         {
46             array_push($lastlines, $event);
47         }
48         unset($event);
49         $event = array();
50         $event[] = htmlspecialchars($line);
51     }
52     else
53     {
54         $event[] = htmlspecialchars($line);
55     }
56 }
57 fclose($fh);
58
59 $lastlines = array_reverse($lastlines);
60 $myhost = gethostname();
61 if($myhost === FALSE)
62 {
63     $myhost = "";
64 }
65
66 echo '<?xml version="1.0" encoding="UTF-8"?>
67 <?xml-stylesheet href="/css/rss.css" type="text/css"?>
68 <rss version="2.0">
69 <channel>
70 <title>OSSEC '.$myhost.' RSS Feed</title>
71 <link>http://ossec.net</link>
72 <description>OSSEC RSS Feed for '.$myhost.'</description>
73 <language>en-us</language>
74 <lastBuildDate>'.date("r", $timelp).'</lastBuildDate>
75 <pubDate>'.date("r", $timelp).'</pubDate>
76 <copyright>(C) OSSEC.net 2008-2011</copyright>
77 <generator>OSSEC.net RSS feed</generator>
78 <ttl>30</ttl>
79 <webMaster>dcid@ossec.net</webMaster>
80
81 <image>
82   <title>OSSEC Alert Feed</title>
83   <url>http://www.ossec.net/img/ossec_logo.jpg</url>
84   <link>http://ossec.net</link>
85 </image>
86 ';
87
88 foreach($lastlines as $myentry)
89 {
90 echo $myentry;
91
92     if(preg_match("/^.. Alert (\d+)\./", $myentry[0], $regs, PREG_OFFSET_CAPTURE, 0))
93     {
94         $myunixtime = $regs[1][0];
95     }
96     else
97     {
98         continue;
99     }
100
101
102     echo '
103     <item>
104         <title>'.$myentry[2]." ,from ".substr($myentry[1], 20).'</title>
105         <link>http://ossec.net</link>
106         <guid isPermaLink="false">'.$myentry[0].'</guid>
107         <description><![CDATA[';
108
109     foreach($myentry as $myline){ echo $myline."<br />\n"; }
110
111     echo '
112         ]]></description>
113         <pubDate>'.date("r", $myunixtime).'</pubDate>
114     </item>
115     ';
116 }
117
118 echo '
119 </channel>
120 </rss>
121 ';
122
123
124 ?>