drush_nagios
nagios.drush.inc
Go to the documentation of this file.
1 <?php
2 
3 include_once 'includes/nagios.inc';
4 
5 /**
6  * @file
7  * The nagios implementation.
8  */
9 
10 /**
11  * @defgroup drush_hooks Drush hooks
12  * Implementation of general drush hooks
13  * @{
14  */
15 
16 
17 /**
18  * Implementation of hook_drush_init().
19  *
20  * Drush version switch for including needed pm_release_recommended()
21  * in drush above v.5 which lives there in a different place.
22  *
23  * @see https://www.drupal.org/node/2358075
24  */
25 function nagios_drush_init() {
26  if ((int)DRUSH_VERSION > 5) {
27  $doc_prefix = drush_get_context('DOC_PREFIX');
28  include_once $doc_prefix . '/commands/pm/updatestatus.pm.inc';
29  }
30 }
31 
32 /**
33  * Implementation of hook_drush_help().
34  * @param
35  * A string with the help section (prepend with 'drush:')
36  * @return
37  * A string with the help text for your command.
38  */
39 function nagios_drush_help($section) {
40  $help_header = dt('
41 
42 Exit status, returned severities:
43 
44 * NAGIOS_OK (0) - if monitored Drupal site is up to date
45 * NAGIOS_WARNING (1) - if there is a code (core, contrib and theme), pending database update available
46  or unmet Drupal requirements with severity \'REQUIREMENT_WARNING\'(1)
47 * NAGIOS_CRITICAL(2) - if there is a code update with severity \'Security update\' available
48  or unmet Drupal requirements with severity \'REQUIREMENT_ERROR\'(2)
49 ');
50  switch ($section) {
51  case 'meta:nagios:title':
52  return dt('Nagios commands');
53  case 'drush:check-updates':
54  $help = dt('Checks a Drupal site for code (core and contrib) updates.');
55  return $help . $help_header;
56  break;
57  case 'drush:check-db-updates':
58  $help = dt('Checks a Drupal site for pending database updates.');
59  return $help . $help_header;
60  break;
61  }
62 }
63 
64 /**
65  * Implementation of hook_drush_command().
66  * @See drush_parse_command() for a list of recognized keys.
67  * @return
68  * An associative array describing your command(s).
69  */
71  $items = array();
72 
73  $items['check-updates'] = array(
74  'callback' => 'drush_nagios_updates',
75  'description' => 'For the usage as nagios plugin: Monitores a Drupal site and print a message and return with an exit status.',
76  'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
77  'examples' => array(
78  'drush @example check-updates' => '/path/to/drupal/sites/default: UPDATES: admin_menu,ckeditor,ckeditor_link,conditional_fields,date,devel,devel_themer,entity,features,field_collection,link,menu_attributes,menu_link,references,transliteration,views_datasource,webform SECURITY UPDATES: ctools,email',
79  'drush @example check-updates --ignore=ctools,email' => '/path/to/drupal/sites/default: UPDATES: admin_menu,ckeditor,ckeditor_link,conditional_fields,date,devel,devel_themer,entity,features,field_collection,link,menu_attributes,menu_link,references,transliteration,views_datasource,webform IGNORED BY SETTINGS: ctools(SECURITY UPDATE available),email(SECURITY UPDATE available)',
80  ),
81  'options' => array(
82  'ignore' => array(
83  'description' => 'Project or comma seperated list of projects which should be ignored.',
84  'value' => 'required',
85  'example-value' => 'ctools,email',
86  ),
87  'ignore-locked' => array(
88  'description' => 'Ignore projects which are locked by drush pm-update --lock.',
89  ),
90  /** @todo Implement recommended-version option in @ref _nagios_print_message().*/
91  /*
92  'recommended-version' => array(
93  'value' => 'optional',
94  'description' => 'Show recomended version for affected projects .',
95  ),
96  */
97  /** @todo Implement changelog option in @ref _nagios_print_message(). */
98  /*
99  'changelog' => array(
100  'value' => 'optional',
101  'description' => 'Show uri of changelog for affected projects.',
102  ),
103  */
104  ),
105  );
106  $items['check-db-updates'] = array(
107  'callback' => 'drush_nagios_updates',
108  'description' => 'Checks for pending database updates.',
109  'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
110  'examples' => array(
111  'drush @example check-db-updates' => '\'/path/to/drupal/sites/default: has pending database updates, run \'drush updatedb\' or visit update.php in your browser for more details.\' with exit status 1',
112  ),
113  /**
114  * @todo Implement option severity for check-db-updates command
115  */
116  /*
117  'options' => array(
118  'severity' => array(
119  'description' => 'Severity if there are pending database updates',
120  'value' => 'required',
121  'example-value' => '1,2',
122  ),
123  ),
124  */
125  );
126  $items['check-drupal-requirements'] = array(
127  'callback' => 'drush_nagios_updates',
128  'description' => 'Checks for anything under Drupals status report.',
129  'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
130  'options' => array(
131  'ignore' => array(
132  'description' => 'Facility or comma seperated list of facilities which should be ignored,',
133  'value' => 'required',
134  'example-value' => 'unicode',
135  ),
136  ),
137  );
138  return $items;
139 }
140 /** @} */
141 
142 /**
143  * @defgroup drush_callbacks Drush callbacks
144  * Implementation of general drush callbacks
145  * @{
146  */
147 
148 /**
149  * Command callback, print status message
150  * and exit with exit status
151  *
152  * @return int
153  * Exit status of the referenced functionn
154  */
156  $command = drush_get_command();
157  if ($command['command'] == 'check-db-updates') {
158  $parsed_info = _nagios_get_db_update_info();
159  }
160  else if ($command['command'] == 'check-updates') {
161  $info = _nagios_get_update_info();
162  $parsed_info = _nagios_parse_update_info($info);
163  }
164  else if ($command['command'] == 'check-drupal-requirements') {
166  $parsed_info = _nagios_parse_drupal_status_info($info);
167  }
168  _nagios_print_message($parsed_info);
169  drush_set_context('DRUSH_EXECUTION_COMPLETED', TRUE);
170  exit(_nagios_exit_status($parsed_info));
171 }
172 /** @} */
_nagios_exit_status($parsed_update_info)
Definition: nagios.inc:847
_nagios_print_message($parsed_update_info)
Definition: nagios.inc:731
_nagios_parse_drupal_status_info($requirements)
Definition: nagios.inc:699
_nagios_get_update_info()
Definition: nagios.inc:52
drush_nagios_updates()
_nagios_get_db_update_info()
Definition: nagios.inc:164
nagios_drush_init()
nagios_drush_help($section)
nagios_drush_command()
_nagios_get_drupal_status_info()
Definition: nagios.inc:628
_nagios_parse_update_info($update_info)
Definition: nagios.inc:346