parsepeers.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. function mail_tpl($from, $to, $template, $fields,$customheaders="") {
  3. global $errno,$er;
  4. // $er->log(ERROR_LEVEL_FPUT,"mail_tpl",array("from"=>$from,"to"=>$to,"template"=>$template));
  5. // echo "mailtpl : $from : $to : $template <br>\n";
  6. /* Envoi d'un mail avec substitution de patron */
  7. $f=@fopen($template,"rb");
  8. if ($f) {
  9. $subject=trim(fgets($f,1024));
  10. $text="";
  11. while (!feof($f)) {
  12. $text.=fgets($f,1024);
  13. }
  14. fclose($f);
  15. reset($fields);
  16. while (list($k,$v)=each($fields)) {
  17. $subject=str_replace("%%".$k."%%",$v,$subject);
  18. $text=str_replace("%%".$k."%%",$v,$text);
  19. }
  20. ini_set('sendmail_from', $from); //Suggested by "Some Guy"
  21. return mail($to,$subject,$text,"From: $from\nReply-to: $from".$customheaders);
  22. } else {
  23. return false;
  24. }
  25. }
  26. function startElement($parser, $name, $attrs) {
  27. global $pfend,$pfattrs,$pfelement;
  28. // echo "start: $name \n";
  29. if ($name=="PEER") {
  30. $pfattrs=array();
  31. if ($attrs["IX"]) $pfattrs["IX"]=$attrs["IX"];
  32. }
  33. $pfelement="";
  34. }
  35. function endElement($parser, $name) {
  36. global $pfend,$pfattrs,$pfelement,$peers;
  37. // echo "end: $name\n";
  38. if ($name=="PEER") {
  39. $peers[]=$pfattrs;
  40. echo ".";
  41. } else {
  42. $pfattrs[$name]=$pfelement;
  43. }
  44. }
  45. function dataParser($parser,$data) {
  46. global $pfend,$pfattrs,$pfelement;
  47. // echo "data:$data\n";
  48. $pfelement.=$data;
  49. }
  50. function parsehead($file) {
  51. global $pfend,$pfattrs,$pfelement;
  52. $pfend=false;
  53. $pfelement="";
  54. $pfattrs=array();
  55. $xml_parser = xml_parser_create("ISO-8859-1");
  56. xml_set_element_handler($xml_parser, "startElement", "endElement");
  57. xml_set_character_data_handler($xml_parser, "dataParser");
  58. if (!($fp = fopen($file, "r"))) {
  59. die("could not open XML input");
  60. }
  61. while (($data = fgets($fp, 4096))) {
  62. if (!xml_parse($xml_parser, $data, gzeof($fp))) {
  63. // return false;
  64. die(sprintf("XML error: %s at line %d",
  65. xml_error_string(xml_get_error_code($xml_parser)),
  66. xml_get_current_line_number($xml_parser)));
  67. }
  68. }
  69. fclose($fp);
  70. xml_parser_free($xml_parser);
  71. reset($pfattrs);
  72. return $pfattrs;
  73. }
  74. echo "Parsing XML file "; flush();
  75. parsehead("peers.xml");
  76. echo " done \n";
  77. echo "Searching for down hosts from file down.txt\n";
  78. $f=fopen("down.txt","rb");
  79. $down=array();
  80. while ($s=fgets($f,1024)) {
  81. $s=trim($s);
  82. $down[$s]=$s;
  83. }
  84. fclose($f);
  85. //print_r($down);
  86. $IPS=array(
  87. "freeix"=>"213.228.3.249",
  88. "sfinx"=>"0",
  89. "panap"=>"62.35.254.66",
  90. );
  91. foreach($peers as $p) {
  92. if ($down[$p["IP"]] && trim($p["CONTACT"])!="") {
  93. echo "Sending mail to peer ".$p["IP"]." for IX ".$p["IX"]." named ".$p["NAME"]." at email ".$p["CONTACT"]."\n";
  94. $p["GITOYENIP"]=$IPS[$p["IX"]];
  95. mail_tpl("noc@gitoyen.net",$p["CONTACT"],"relance.mailtpl.txt",$p);
  96. }
  97. }
  98. ?>