root/trunk/wifidog-auth/wifidog/cron/page.php

Revision 1326, 7.4 KB (checked in by dana, 14 years ago)

Added require for Mail.php

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5// +-------------------------------------------------------------------+
6// | WiFiDog Authentication Server                                     |
7// | =============================                                     |
8// |                                                                   |
9// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
10// | portal suite.                                                     |
11// +-------------------------------------------------------------------+
12// | PHP version 5 required.                                           |
13// +-------------------------------------------------------------------+
14// | Homepage:     http://www.wifidog.org/                             |
15// | Source Forge: http://sourceforge.net/projects/wifidog/            |
16// +-------------------------------------------------------------------+
17// | This program is free software; you can redistribute it and/or     |
18// | modify it under the terms of the GNU General Public License as    |
19// | published by the Free Software Foundation; either version 2 of    |
20// | the License, or (at your option) any later version.               |
21// |                                                                   |
22// | This program is distributed in the hope that it will be useful,   |
23// | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
24// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
25// | GNU General Public License for more details.                      |
26// |                                                                   |
27// | You should have received a copy of the GNU General Public License |
28// | along with this program; if not, contact:                         |
29// |                                                                   |
30// | Free Software Foundation           Voice:  +1-617-542-5942        |
31// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
32// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
33// |                                                                   |
34// +-------------------------------------------------------------------+
35
36/**
37 * @package    WiFiDogAuthServer
38 * @author     Philippe April
39 * @copyright  2005-2006 Philippe April
40 * @version    Subversion $Id$
41 * @link       http://www.wifidog.org/
42 */
43
44 /**
45  * Load required files
46  */
47
48  require_once(dirname(__FILE__) . '/../include/common.php');
49
50  require_once('classes/Network.php');
51  require_once('classes/Node.php');
52  require_once('classes/Mail.php');
53
54  function page_if_down_since($nodeObject, $minutes) {
55      $db = AbstractDb::getObject();
56      $last_heartbeat = strtotime($nodeObject->getLastHeartbeatTimestamp());
57$time = time();
58    $downtime = round((time() - $last_heartbeat)/60, 0);
59    $paged_addresses = "";
60
61      if ($time - $last_heartbeat > 60*$minutes) {//If hostpot is down for longuer than the requested average interval
62          $lastPaged = strtotime($nodeObject->getLastPaged());
63          //echo sprintf("Node down for %f minutes, Last paged: %f minutes ago, difference between last page and last heartbeat: %s, difference must be less than %d minutes to page again <br/>\n", ($time-$last_heartbeat)/60, ($time-$lastPaged)/60, ($lastPaged - $last_heartbeat)/60, $minutes);
64          if (!$nodeObject->getLastPaged() || !$lastPaged) {
65              //If we can't retrieve or parse, pretend we last paged right before the hostpot went down
66              $lastPaged = $last_heartbeat-1;
67          }
68         
69          if ($lastPaged < ($last_heartbeat + 60*$minutes)) {//If we haven't paged since the downtime reached the threshold
70              $network = $nodeObject->getNetwork();
71
72              $nodeObject->setLastPaged(time());
73              $usersToPage = $nodeObject->DEPRECATEDgetTechnicalOfficers();
74              $usersMsg = null;
75              foreach ($usersToPage as $officer) {
76                  # Doesn't work if called from cron
77                  #Locale :: setCurrentLocale(Locale::getObject($officer->getPreferedLocale()));
78                  $mail = new Mail();
79                  $mail->setSenderName(_("Monitoring system"));
80                  $mail->setSenderEmail($network->getTechSupportEmail());
81                  $mail->setRecipientEmail($officer->getEmail());
82                  $mail->setMessageSubject($minutes . " - " . $network->getName()." "._("node")." ".$nodeObject->getName());
83                  $mail->setHighPriority(true);
84                  $mail->setMessageBody(sprintf(_("Node %s (%s) has been down for %d minutes (since %s)"), $nodeObject->getName(), $nodeObject->getId(), $minutes, date("r", $last_heartbeat)));
85                  $mailRetval = $mail->send();
86                  $usersMsg .= sprintf("%s: %s", $officer->getUsername(), $mailRetval?_("Success"):_("Failed sending mail"))."\n";
87              }
88              $msg = sprintf("Node %s has been DOWN for %d minutes, we mailed the following %d user(s):\n%s", $nodeObject->getName(), ($time-$last_heartbeat)/60, count($usersToPage), $usersMsg) ;
89              throw new exception($msg);
90          }
91          throw new exception(sprintf("Node %s has been DOWN for %d minutes (more than %d minutes), but we already notified everyone after %d minutes."."\n",$nodeObject->getName(), ($time-$last_heartbeat)/60, $minutes, ($lastPaged-$last_heartbeat)/60));
92      }
93  }
94
95  try {
96      $sql = "SELECT node_id FROM nodes WHERE node_deployment_status = 'DEPLOYED'";
97      $nodes_results = null;
98      $db = AbstractDb::getObject();
99      $db->execSql($sql, $nodes_results, false);
100
101      if ($nodes_results == null)
102      throw new Exception(_("No deployed nodes could not be found in the database"));
103
104      echo "<html>\n<head>\n";
105      echo "<title>Node Monitoring System</title>\n</head>\n";
106      echo "<style>\n";
107      echo "table {border: 1px solid black;}\n";
108      echo "td {padding: 4px;}\n";
109      echo "tr {border: 1px solid black;}\n";
110      echo ".alert {background: #ffaaaa;}\n";
111      echo "</style>";
112      echo "<body>\n";
113      echo "<p>Current server time: " . date("r") . "</p>";
114      echo "<table>\n<tr><th>Node</th><th>Last Heartbeat</th><th>Last IP Address</th><th>Status Message</th></tr>\n";
115      foreach ($nodes_results as $node_row)
116      {
117        $nodeObject = Node :: getObject($node_row['node_id']);
118          echo "<tr><td>";
119          echo $nodeObject->getName();
120          echo "</td><td>";
121          echo $nodeObject->getLastHeartbeatTimestamp();
122          echo "</td><td>";
123          echo $nodeObject->getLastHeartbeatIP();
124          echo "</td>";
125          try {
126              page_if_down_since($nodeObject, 43200);//A month
127              page_if_down_since($nodeObject, 10080);//A week
128              page_if_down_since($nodeObject, 1440);//A day
129              page_if_down_since($nodeObject, 120);//Two hours
130              page_if_down_since($nodeObject, 30);//30 min
131              page_if_down_since($nodeObject, 5);//5 min
132              echo "<td>ok</td>";
133          } catch (Exception $e) {
134              # Do nothing, we cronned this
135              echo "<td class=\"alert\">";
136              echo $e->getMessage() . "<br>";
137              echo "</td>";
138          }
139          echo "</tr>\n";
140      }
141      echo "</table></body></html>\n";
142  } catch (Exception $e) {
143      echo $e;
144  }
145  /*
146   * Local variables:
147   * tab-width: 4
148   * c-basic-offset: 4
149   * c-hanging-comment-ender-p: nil
150   * End:
151   */
152   ?>
Note: See TracBrowser for help on using the browser.