Statusengine - In Memory

What is Statusengine - In Memory?

With Statusengine's Memcached extension you are able to save Naemon or Nagios status data like host status, service status, downtimes and acknowlegments into a Memcached server.
To access stored data StatusengineMemoryClient provides you a easy to use PHP API, wich allows you to join, limit, order and much more your results.

Notice:

I develop Statusengine in my spare time. I developed the Memcached Extension just for fun and this is a very basic implementation

How to setup?

First of all you need install memcached and php5-memcached on your system.


apt-get install memcached php5-memcached

In the next step, you tell Statusengine to store status, downtime and acknowlegment data in the memory as well.
Simple modify your /opt/statusengine/cakephp/app/Config/Statusengine.php configuration file:



 <?php
'memcached' => [
    
'use_memcached' => true,  // Use memcached or not
    
'processing_type' => 0,   // 0 = save only in memcached, 0 = save in db and memcached
    
'drop_on_start' => false// Clear all memcacehd entries on start up of StatusengineLegacyShell
    
'server' => '127.0.0.1',  // Address of memcached server
    
'port' => 11211           // Port of memcached server
]

Now you just need to restart StatusengineLegacyShell and it will write all status, downtime and acknowlegment data to the MySQL database and to Memcached


service statusengine restart

Query data

StatusengineMemoryClient provides you a very simple PHP API to query your data.
I tried to implement a basic version of CakePHP's Model::find() syntax.
The following examples will show you, how easy you can query your data through StatusengineMemoryClient.php.

Query host status of all your hosts

Code:



<?php
use StatusengineMemory\StatusengineMemoryClient;
require_once 
'/opt/statusengine/MemcachedClient/StatusengineMemoryClient.php';

$StatusengineMemoryClient = new StatusengineMemory\StatusengineMemoryClient('127.0.0.1'11211);

$hoststatusresult $StatusengineMemoryClient->Hoststatus->findAll();
print_r($hoststatusresult);

Result:


Array
(
    [0] => Array
        (
            [Hoststatus] => Array
                (
                    [name] => localhost
                    [status_update_time] => 2015-04-06 22:48:34
                    [output] => OK - 127.0.0.1: rta 0.057ms, lost 0%
                    [long_output] =>
                    [event_handler] =>
                    [perfdata] => rta=0.057ms;3000.000;5000.000;0; pl=0%;80;100;; rtmax=0.057ms;;;; rtmin=0.057ms;;;;
                    [check_command] => 5a538ebc-03de-4ce6-8e32-665b841abde3!3000.0,80%!5000.0,100%
                    [check_period] => 41012866-6114-4853-9caf-6ffd19954e50
                    [current_state] => 0
                    -- Removed rest of the output --
                )

        )

    [1] => Array
        (
            [Hoststatus] => Array
                (
                    [name] => router
                    [status_update_time] => 2015-04-06 22:47:11
                    [output] => OK - 127.0.0.2: rta 0.042ms, lost 0%
                    [long_output] =>
                    [event_handler] =>
                    [perfdata] => rta=0.042ms;3000.000;5000.000;0; pl=0%;80;100;; rtmax=0.042ms;;;; rtmin=0.042ms;;;;
                    [check_command] => 5a538ebc-03de-4ce6-8e32-665b841abde3!3000.0,80%!5000.0,100%
                    [check_period] => 41012866-6114-4853-9caf-6ffd19954e50
                    [current_state] => 0
                    -- Removed rest of the output --
                )

        )

)

Select fields or set conditions to filter the result

May be you don't want to select the full host status object, so you are able to select the fields you need.
Additionaly you can order by a given field.

Code:


<?php
use StatusengineMemory\StatusengineMemoryClient;
require_once 
'/opt/statusengine/MemcachedClient/StatusengineMemoryClient.php';

$StatusengineMemoryClient = new StatusengineMemory\StatusengineMemoryClient('127.0.0.1'11211);

$hoststatusresult $StatusengineMemoryClient->Hoststatus->findAll([], [
    
'conditions' => [
        
'name' => 'router'
    
],
    
'fields' => [
        
'name',
        
'output',
        
'current_state'
    
],
    
'order' => [
        
'current_state' => 'ASC'
        
]
]);
print_r($hoststatusresult);

Result:


Array
(
    [0] => Array
        (
            [Hoststatus] => Array
                (
                    [name] => router
                    [output] => OK - 127.0.0.2: rta 0.042ms, lost 0%
                    [current_state] => 0
                )

        )

)

Select host status by host name/s

Query a single host by it's name:
Code:


<?php
$query 
= [
    
'fields' => ['name']
];
$hoststatusresult $StatusengineMemoryClient->Hoststatus->find('router'$query);
print_r($hoststatusresult);

Result:


Array
(
    [Hoststatus] => Array
        (
            [name] => router
        )

)


Select multiple hosts
Code:


<?php
$query 
= [
    
'fields' => ['name'],
];
$hoststatusresult $StatusengineMemoryClient->Hoststatus->findAll([
    
'router',
    
'localhost'
], $query);
print_r($hoststatusresult);

Result:


Array
(
    [0] => Array
        (
            [Hoststatus] => Array
                (
                    [name] => router
                )

        )

    [1] => Array
        (
            [Hoststatus] => Array
                (
                    [name] => localhost
                )

        )

)

Query service status of all your services

Code:


<?php
use StatusengineMemory\StatusengineMemoryClient;
require_once 
'/opt/statusengine/MemcachedClient/StatusengineMemoryClient.php';

$StatusengineMemoryClient = new StatusengineMemory\StatusengineMemoryClient('127.0.0.1'11211);


$servicestatusresult $StatusengineMemoryClient->Servicestatus->findAll([], [
    
'fields' => [
    
'host_name',
    
'description'
]);
print_r($servicestatusresult);

Result:

Array
(
    [0] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => localhost
                    [description] => broken
                )

        )

    [1] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => router
                    [description] => Ping
                )

        )

    [2] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => localhost
                    [description] => CHECK_LOCAL_LOAD
                )

        )

    [3] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => localhost
                    [description] => CHECK_LOCAL_USERS
                )

        )

)

Select service status by host name/s

Code:


<?php
$servicestatusresult 
$StatusengineMemoryClient->Servicestatus->findAll(
    [    
//Param 1
        
'localhost' => [ //Host name
            
'Ping',      //Service desc 1
            
'broken'     //Service desc 2
        
],
        
'router' => [
            
'Ping'
        
]
    ],

    [    
//Param 2
        
'fields' => [
            
'host_name',
            
'description',
            
'output'
        
],
    ]
);
print_r($servicestatusresult);

Result:


Array
(
    [0] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => localhost
                    [description] => Ping
                    [output] => PING OK - Packet loss = 0%, RTA = 0.06 ms
                )

        )

    [1] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => localhost
                    [description] => broken
                    [output] => Im broken :(
                )

        )

    [2] => Array
        (
            [Servicestatus] => Array
                (
                    [host_name] => router
                    [description] => Ping
                    [output] => PING OK - Packet loss = 0%, RTA = 0.60 ms
                )

        )

)

Select a single service

Code:


<?php
$StatusengineMemoryClient 
= new StatusengineMemory\StatusengineMemoryClient('127.0.0.1'11211);

$query = [
    
'fields' => [
        
'host_name',
        
'description',
        
'output'
    
],
];
$servicestatusresult $StatusengineMemoryClient->Servicestatus->find('localhost''Ping'$query);
print_r($servicestatusresult);

Result:


Array
(
    [Servicestatus] => Array
        (
            [host_name] => localhost
            [description] => Ping
            [output] => PING OK - Packet loss = 0%, RTA = 0.06 ms
        )
)


Join Downtimes and Acknowledgements

To access downtime or acknowlegment data you need to join them to your result:

Code:


<?php
$query 
= [
    
'join' => [
        
'Downtime',
        
'Acknowledgement'
    
]
];
$servicestatusresult $StatusengineMemoryClient->Servicestatus->find('localhost''broken'$query);
print_r($servicestatusresult);

Result:


Array
(
    [Servicestatus] => Array
        (
            [host_name] => localhost
            [description] => broken
            [status_update_time] => 2015-04-07 00:16:27
            [output] => Im broken :(
            [long_output] =>
            [event_handler] =>
            [perfdata] =>
            -- Removed rest of the output --
        )

    [Downtime] => Array
        (
            [host_name] => localhost
            [service_description] => broken
            [author_name] => Daniel Ziegler
            [comment_data] => And in Downtime, because sooo broken
            [downtime_type] => 1
            [entry_time] => 2015-04-07 00:16:27
            [start_time] => 2015-04-07 00:04:00
            [end_time] => 2015-04-10 12:04:00
            [triggered_by] => 0
            [downtime_id] => 20
            [fixed] => 1
            [duration] => 302400
            [was_started] => 1
            [actual_start_time] => 1970-01-01 01:00:00
            [actual_start_time_usec] => 0
            [actual_end_time] => 2015-04-07 00:16:27
            [actual_end_time_usec] => 1428358587
        )

    [Acknowledgement] => Array
        (
            [host_name] => localhost
            [service_description] => broken
            [entry_time] => 2015-04-07 00:15:05
            [entry_time_usec] => 1428358505
            [author_name] => Daniel Ziegler
            [comment_data] => This service is so broken :(
            [acknowledgement_type] => 1
            [state] => 2
            [is_sticky] => 0
            [persistent_comment] => 1
            [notify_contacts] => 1
        )

)

Filter join results

Code:


<?php
$StatusengineMemoryClient 
= new StatusengineMemory\StatusengineMemoryClient('127.0.0.1'11211);

$query = [
    
'fields' => [
        
'Servicestatus.host_name',
        
'Servicestatus.description',
        
'Servicestatus.output',
        
'Downtime.author_name',
        
'Downtime.comment_data',
        
'Acknowledgement.author_name',
        
'Acknowledgement.comment_data'
    
],
    
'conditions' => [
        
'Servicestatus.current_state' => [1,2,3]
    ],
    
'join' => [
        
'Downtime',
        
'Acknowledgement'
    
]
];
$servicestatusresult $StatusengineMemoryClient->Servicestatus->find('localhost''broken'$query);
print_r($servicestatusresult);

Result:


Array
(
[Servicestatus] => Array
    (
        [host_name] => localhost
        [description] => broken
        [output] => Im broken :(
    )

[Downtime] => Array
    (
        [author_name] => Daniel Ziegler
        [comment_data] => And in Downtime, because sooo broken
    )

[Acknowledgement] => Array
    (
        [author_name] => Daniel Ziegler
        [comment_data] => This service is so broken :(
    )
)

Overview of all options

Code:



<?php
$query 
= [
    
'fields' => [
        
'Servicestatus.host_name',      // Set all fields you like to query
        
'Servicestatus.description',    // Syntax: Modelname.Fieldname
        
'Servicestatus.output',         // If you dont set a model name, the Client will
        
'Servicestatus.current_state',  // use Servicestatus or Hoststatus as default model name.
        
'Downtime.author_name',         // If you call Hoststatus->find() default modle name is Hoststatus.xxx
        
'Downtime.comment_data',        // and if you call Servicestatus->find, default model name is Servicestatus.xxx
        
'Acknowledgement.author_name',
        
'Acknowledgement.comment_data'
    
],
    
'conditions' => [
        
'Servicestatus.current_state' => [1,2,3// Behaves like MySQL WHERE current_state IN (1,2,3)
        
'Servicestatus.current_state' => 1       // Behaves like MySQL WHERE current_state = 1
    
],
    
'join' => [
        
'Downtime',
        
'Acknowledgement'
    
],
    
'limit' => 100      // Limit result length to 100 records
    
'limit' => [50,100// Start at record 50 and return to record 100
    
'order' => [
        
'Servicestatus.current_state' => 'ASC'
        'Servicestatus.current_state' 
=> 'DESC'
    
]
];

Client license

StatusengineMemoryClient is dual licensed under the MIT or GPL Version 2 licenses


Nagios, NDOUtils and the Nagios logo are trademarks, servicemarks, registered trademarks or registered servicemarks owned by Nagios Enterprises, LLC. All other trademarks, servicemarks, registered trademarks, and registered servicemarks are the property of their respective owner(s).
All other trademarks are property of their respective owners. Other product or company names mentioned may be trademarks or trade names of their respective owner.