r44397 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44396‎ | r44397 | r44398 >
Date:14:32, 10 December 2008
Author:harddisk
Status:deferred
Tags:
Comment:
refitted alertbot to use MySQL backend. not finished yet =)
Modified paths:
  • /trunk/tools/alertbot/irc/alertbot.php (modified) (history)
  • /trunk/tools/alertbot/web/addwarn.php (modified) (history)
  • /trunk/tools/alertbot/web/data.inc.php (modified) (history)
  • /trunk/tools/alertbot/web/db.sql (added) (history)
  • /trunk/tools/alertbot/web/warning.php (modified) (history)

Diff [purge]

Index: trunk/tools/alertbot/irc/alertbot.php
@@ -30,34 +30,30 @@
3131
3232 //configuration
3333 include("config.inc.php");
34 -
 34+ include("../web/config.php");
 35+ include("../web/data.inc.php");
3536 class alertbot
3637 {
3738 function check_msgs(&$irc) { //check for new reports
38 - global $warnings;global $admins;global $channel;
39 - if(!file_exists("/tmp/ab_msg.txt")) return;
40 - $file=file("/tmp/ab_msg.txt");
41 - unlink("/tmp/ab_msg.txt");
42 - if(sizeof($file)>0) {
43 - for($i=0;$i<sizeof($file);$i++) {
44 - $msg=explode(chr(250),trim($file[$i]));
45 - $user=$msg[1];$msg=$msg[0];
46 - if(strlen($msg)<=5) break;
47 - $wid=sizeof($warnings)+1;
48 - $warnings[$wid]["text"]=$msg;
 39+ global $warnings;global $admins;global $channel;global $mysql_link;
 40+ $res=mysqli_query($mysql_link,"SELECT * FROM alerts WHERE state = 0 ORDER BY time");
 41+
 42+ if(mysqli_num_rows($res)>0) {
 43+ while($warn=mysqli_fetch_assoc($res)) {
 44+ $wid=$warn["id"];
 45+ $warnings[$wid]["text"]=$warn["problem"];
4946 $warnings[$wid]["time"]=time();
5047 $warnings[$wid]["id"]=$wid;
51 - $warnings[$wid]["action"]="PENDING";
52 - $warnings[$wid]["user"]=$user;
53 - $irc->message(SMARTIRC_TYPE_CHANNEL, $channel,"$admins: $msg (id $wid)");
54 - echo "dumped $msg\n";
 48+ $warnings[$wid]["action"]=$warn["state"];
 49+ $warnings[$wid]["user"]=$warn["reporter"];
 50+ $irc->message(SMARTIRC_TYPE_CHANNEL, $channel,"$admins: ".$warn["project"]." (id $wid, pr ".$warn["affected"].")");
 51+ echo "dumped warning $wid\n";
5552 }
5653 }
57 - $fp=fopen("/tmp/ab_dump.txt","w");fwrite($fp,serialize($warnings));fclose($fp);
5854 }
5955
6056 function check_rep(&$irc) { //check for warnings whose warning time expired, and send SMS
61 - global $warnings;global $channel;global $ttw;global $lastsms;
 57+ global $warnings;global $channel;global $ttw;global $lastsms;global $mysql_link;
6258 if(sizeof($warnings)<1) return; // less than one warning in the queue? don't even bother to scan it
6359 foreach($warnings as $warning) {
6460 if(time()-$warning["time"]>=$ttw && $warning["alerted"]!=true && time()-$lastsms>=$smsinterval) { //warning is older than TTL, and didn't get SMSed before, and there was no SMS for 10 minutes
@@ -65,7 +61,7 @@
6662 // alert($warning["text"]); //send SMS
6763 $warnings[$warning["id"]]["alerted"]=true; //mark as "sms sent"
6864 $warnings[$warning["id"]]["action"]="SMS SENT";
69 - $fp=fopen("/tmp/ab_dump.txt","w");fwrite($fp,serialize($warnings));fclose($fp);
 65+ mysqli_query($mysql_link,"UPDATE `alertbot`.`alerts` SET `state` = '2' WHERE `alerts`.`id` =".$warning["id"]." LIMIT 1 ;");
7066 }
7167 }
7268 }
@@ -74,7 +70,7 @@
7571 global $warnings;global $channel;
7672 if(isset($warnings[$ircdata->messageex[1]])) {
7773 unset($warnings[$ircdata->messageex[1]]);
78 - $fp=fopen("/tmp/ab_dump.txt","w");fwrite($fp,serialize($warnings));fclose($fp);
 74+ mysqli_query($mysql_link,"UPDATE `alertbot`.`alerts` SET `state` = '4' WHERE `alerts`.`id` =".$ircdata->messageex[1]." LIMIT 1 ;");
7975 $irc->message(SMARTIRC_TYPE_CHANNEL, $channel,"Killed ".$ircdata->messageex[1]);
8076 }
8177 }
@@ -99,7 +95,7 @@
10096 if(isset($warnings[$ircdata->messageex[1]])) {
10197 $warnings[$ircdata->messageex[1]]["alerted"]=true; //so that no sms gets sent
10298 $warnings[$ircdata->messageex[1]]["action"]="ACKd";
103 - $fp=fopen("/tmp/ab_dump.txt","w");fwrite($fp,serialize($warnings));fclose($fp);
 99+ mysqli_query($mysql_link,"UPDATE `alertbot`.`alerts` SET `state` = '1' WHERE `alerts`.`id` =".$ircdata->messageex[1]." LIMIT 1 ;");
104100 $irc->message(SMARTIRC_TYPE_CHANNEL, $channel,"Killed ".$ircdata->messageex[1]);
105101 }
106102 }
Index: trunk/tools/alertbot/web/db.sql
@@ -0,0 +1,10 @@
 2+CREATE TABLE IF NOT EXISTS `alerts` (
 3+ `id` int(11) NOT NULL auto_increment,
 4+ `time` timestamp NOT NULL,
 5+ `reporter` varchar(10) NOT NULL,
 6+ `project` varchar(20) NOT NULL,
 7+ `affected` varchar(100) NOT NULL,
 8+ `problem` text NOT NULL,
 9+ `state` int(11) NOT NULL default '0',
 10+ PRIMARY KEY (`id`)
 11+);
Property changes on: trunk/tools/alertbot/web/db.sql
___________________________________________________________________
Added: svn:eol-style
112 + native
Index: trunk/tools/alertbot/web/addwarn.php
@@ -8,17 +8,23 @@
99 <body>
1010 <?PHP
1111 require("data.inc.php");
12 -$problem=$_REQUEST["problem"];
13 -$project=$_REQUEST["project"];
14 -$language=$_REQUEST["language"];
15 -$level=$_REQUEST["affected"];
16 -$problem_other=$_REQUEST["problem-other"];
17 -$banlist=file("/tmp/ab_bans.txt");
 12+require("config.php");
1813
 14+$problem=mysqli_real_escape_string($mysql_link,$_REQUEST["problem"]);
 15+$project=mysqli_real_escape_string($mysql_link,$_REQUEST["project"]);
 16+$language=mysqli_real_escape_string($mysql_link,$_REQUEST["language"]);
 17+$level=mysqli_real_escape_string($mysql_link,$_REQUEST["affected"]);
 18+$problem_other=mysqli_real_escape_string($mysql_link,$_REQUEST["problem-other"]);
 19+
1920 //prechecks for sanity
20 -foreach($banlist as $ban) //check if user is banned
21 - if($_SERVER['REMOTE_ADDR']==trim($ban))
22 - $msg.="You are banned from using this service.";
 21+$res=mysqli_query($mysql_link,"SELECT * FROM bans");
 22+if(mysqli_num_rows($res)>0) {
 23+ while($ban=mysqli_fetch_assoc($res)) {
 24+ foreach($banlist as $ban) //check if user is banned
 25+ if($_SERVER['REMOTE_ADDR']==trim($ban["host"]))
 26+ $msg.="You were banned from using this service by ".$ban["user"].".";
 27+ }
 28+}
2329 if(array_search($project,array_keys($acceptable_projects))===FALSE)
2430 $msg.="Please choose a valid project.<br />\r\n";
2531 if(array_search($problem,array_keys($acceptable_problems))===FALSE)
@@ -45,13 +51,13 @@
4652 else
4753 $project=$language.".".$project;
4854
49 - $sms=$level_correspond[$level].": ".$project." / ".$problem_correspond[$problem];
 55+ $problem=$problem_correspond[$problem];
5056 if($problem=="other" || $problem=="parts")
51 - $sms.=$problem_other;
52 - $fp=fopen("/tmp/ab_msg.txt","a");
53 - fwrite($fp,$sms.chr(250).$_SERVER['REMOTE_ADDR']."\r\n");
54 - fclose($fp);
55 - $msg.="Thanks for your submission.<br />\r\nMessage sent: $sms<br />\r\n";
 57+ $problem.=$problem_other;
 58+ $res=mysqli_query($mysql_link,"INSERT INTO `alertbot`.`alerts` (`id`, `time`, `reporter`, `project`, `affected`, `problem`, `state`) VALUES (NULL, CURRENT_TIMESTAMP, '".$_SERVER['REMOTE_ADDR']."', '$project', '".$level_correspond[$level]."', '".$problem."', '0');");
 59+ if(mysqli_affected_rows($mysql_link)!=1)
 60+ $msg.="Error occurred: ".mysqli_error($mysql_link)." (".mysqli_errno($mysql_link).")";
 61+ $msg.="Thanks for your submission.<br />\r\n"."INSERT INTO `alertbot`.`alerts` (`id`, `time`, `reporter`, `project`, `affected`, `problem`, `state`) VALUES (NULL, CURRENT_TIMESTAMP, '".$_SERVER['REMOTE_ADDR']."', '$project', '".$level_correspond[$level]."', '".$problem."', '0');";
5662 }
5763 $msg.="<a href='warning.php'>Back</a> to main page.";
5864 ?>
Index: trunk/tools/alertbot/web/data.inc.php
@@ -16,5 +16,5 @@
1717 $problem_correspond=array( "sitedown"=>"Site totally down","slow"=>"Site abormally slow",
1818 "replag"=>"Excessive replication lag", "parts"=>"Parts of site do not work:",
1919 "other"=>"" );
20 -
 20+$states=array( 0=>"fresh",1=>"acknowledged",2=>"sms sent",3=>"fixed",4=>"spam" );
2121 ?>
\ No newline at end of file
Index: trunk/tools/alertbot/web/warning.php
@@ -21,6 +21,7 @@
2222 </script>
2323 <?PHP
2424 require("data.inc.php");
 25+require("config.php");
2526 ?>
2627 </head>
2728
@@ -70,11 +71,13 @@
7172 Current problems:
7273 <table><tr><th>ID</th><th>Time</th><th>Problem</th><th>Status</th></tr>
7374 <?PHP
74 - $warnings=unserialize(file_get_contents("/tmp/ab_dump.txt"));
75 - if(sizeof($warnings)>0) {
76 - foreach($warnings as $warn) {
77 - echo "<tr><td>".$warn["id"]."</td><td>".date("j.m.Y h:i:s",$warn["time"])."</td><td>".$warn["text"]."</td><td>".$warn["action"]."</td></tr>";
78 - }
 75+
 76+ $res=mysqli_query($mysql_link,"SELECT * FROM alerts WHERE state = 0 OR state= 1 OR state= 2 ORDER BY time");
 77+
 78+ if(mysqli_num_rows($res)>0) {
 79+ while($warn=mysqli_fetch_assoc($res)) {
 80+ echo "<tr><td>".$warn["id"]."</td><td>".$warn["time"]."</td><td>".$warn["problem"]."</td><td>".$states[$warn["state"]]."</td></tr>";
 81+ }
7982 } else {
8083 echo "<tr><td colspan=\"4\">No current problems known to the Emergency tracker.</td></tr>";
8184 }

Status & tagging log