Search This Blog

Wednesday, March 27, 2013

Symfony Project sfservermon Post 6 - Update server status.

Updating the Server status.

The previous post allow us to create a new updater class and run it from the command line:

[johnr@sentos4 webserver]$ app/console servermon:update

It also talked about the initialisation of the ServerUpdate class using dependency injection as a Symfony service.

This post documents the actual update process, and involves 2 classes:
  • ServerUpdate - which uses the UpdaterStatus class to check the status for all required servers.
  • UpdaterStatus - updates a status for one particular server, it also provides a notify method to do the required notification.
At first thought much of the ServerUpdate code could be included in the UpdateCommand class - however the Symfony documentation for the console class reccommends that the command class do very little other than check any required parameters - for this application at least this is a very good reason - as an update can be initiated from the update action in the default Controller.

Much of this code is a straight port (read copy and paste) from the excellent code in phpServerMon application.

ServerUpdate Class.

This class has an update method: called from the updateCommand or the updateAction:

$serverUpdate =  $this->container = 

It does the following:
  1. Creates an array $servers[] of the required servers to update
  2. Reads the application configuration.
  3. Creates a Monitor log class.
  4. Creates a new updaterStatus class.
  5. Loops through each server: 
Reading the Required Servers:
If a server label is specified read that one or read all active servers:

if ($serverLabel) {
$servers = $this->em->getRepository('JMPRServerMonBundle:MonitorServers')->findByLabel($serverLabel);
} else {
$servers = $this->em->getRepository('JMPRServerMonBundle:MonitorServers')->findByActive('yes');

Getting the Configuration and Creating the required Classes.

//get Configuration.
$configuration = $this->em->getRepository('JMPRServerMonBundle:MonitorConfig')->getConfig();

$monitorLog = $this->em->getRepository('JMPRServerMonBundle:MonitorLog');

$updater = new UpdaterStatus($this->em, $configuration, $monitorLog);

The configuration data requires helper class in its repository: - getConfig.
The configuration is stored (persisted) as a series of database records - 1 record for each configuration setting, but is used throughout the application as an associative array:

public function getConfig() {
$configs = $this->findAll();
foreach($configs as $config) {
$configArray[$config->getKey()] = $config->getValue();
return $configArray;

Main Server loop:

This is where the work happens:
  1. Saving the old status
  2. Calling UpdateStatus to updating it
  3. Notifying and
  4. Updating the server entity

(1) $status_org = $server->getStatus();

// remove the old status from the array to avoid confusion between the new and old status

(2) $updater->setServer($server, $status_org);

//check server status
$status_new = $updater->getStatus();

(3) //notify the nerds of applicable

//update server status
(4) $server->setLastCheck(new \DateTime('now'));

if ($status_new = 'on') {

The next post goes into details on the Updater Status class.

No comments:

Post a Comment