Drupal module to report status of various network systems. Will automatically check websites and report results to users.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

systemstatus.module 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Creates block selecting a random YouTube video from a given list
  6. */
  7. /**
  8. * Valid permissions for this module
  9. * @return array An array of valid permissions for the systemstatus module
  10. */
  11. function systemstatus_perm() {
  12. return array( 'administer system status reporter', 'update system statuses' );
  13. }
  14. /**
  15. *Create the admin form
  16. */
  17. function systemstatus_admin() {
  18. $form = array();
  19. $form['settings']['systemstatus_blocktitle'] = array(
  20. '#type' => 'textfield',
  21. '#title' => t( 'Block Title' ),
  22. '#default_value' => variable_get( 'systemstatus_blocktitle', 'Service Status' ),
  23. '#size' => 20,
  24. '#description' => t( 'Enter the title for the block to be displayed to the user (you can also override this in the block configuration).'),
  25. '#required' => FALSE,
  26. );
  27. $form['systemstatus_systems'] = array(
  28. '#type' => 'textarea',
  29. '#title' => t( 'Systems List' ),
  30. '#default_value' => variable_get( 'systemstatus_systems', '' ),
  31. '#cols' => 60,
  32. '#rows' => 10,
  33. '#resizable' => TRUE,
  34. '#description' => t( 'List of Systems to check. For web (http) systems enter as <em>[system name],[url]</em>. Only one system per line.' ),
  35. '#required' => TRUE,
  36. '#wysiwyg' => FALSE,
  37. );
  38. return system_settings_form( $form );
  39. }
  40. /**
  41. *Create the menu item for the menu page
  42. */
  43. function systemstatus_menu() {
  44. $items = array();
  45. $items['admin/settings/systemstatus'] = array(
  46. 'title' => 'System Status Reporter Settings',
  47. 'description' => 'Administer System Status Reporter Settings',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array( 'systemstatus_admin' ), //call the options function
  50. 'access arguments' => array( 'administer system status reporter' ), //which permission accesses
  51. 'type' => MENU_NORMAL_ITEM,
  52. );
  53. return $items;
  54. }
  55. /**
  56. *Validate user input
  57. */
  58. function systemstatus_admin_validate( $form, &$form_state ) {
  59. @ini_set( 'auto_detect_line_endings', true ); //standardize line endings
  60. $systemsArray = explode( PHP_EOL, $form_state['values']['systemstatus_systems'] );
  61. $line = 1;
  62. foreach ( $systemsArray as $system ) {
  63. if ( strstr( $system, ',' ) !== false ) { //make sure there is a URL
  64. $sysAttr = explode( ',', $system ); //split the url
  65. if ( sizeof( $sysAttr ) > 2 ) { //throw an error if we put in too many commas
  66. form_set_error( 'systemstatus_systems', t( 'The entry <em>' . $system . '</em> on line ' . $line . ' does not appear to be valid.' ) );
  67. }
  68. if ( ! valid_url( trim( $sysAttr[1] ), true ) ) { //verify the URL is valid, return error if not.
  69. form_set_error( 'systemstatus_systems', t( 'The URL <em>' . $sysAttr[1] . '</em> on line ' . $line . ' does not appear to be a valid url.' ) );
  70. }
  71. $sysTitle = $sysAttr[0]; //Set the system title to validate later
  72. } else {
  73. $sysTitle = $system; //Set the system title to validate later
  74. }
  75. if ( systemstatus_sanitize_string( $sysTitle ) === false ) { //make sure the system title is a valid string
  76. form_set_error( 'systemstatus_systems', t( 'The System <em>' . $sysTitle . '</em> on line ' . $line . ' does not appear to be a valid system name. System names must can contain only letters, numbers, and spaces.' ) );
  77. }
  78. $line++;
  79. }
  80. }
  81. /**
  82. * validates strings for input
  83. */
  84. function systemstatus_sanitize_string( $string ) {
  85. $filtered = filter_var( $string, FILTER_SANITIZE_STRING ); //create a filtered version of the string
  86. return strcmp( $string, $filtered ) == 0 ? true : false; //return true if the filter didn't change anything. False if it did.
  87. }
  88. /**
  89. *Create a block for displaying the status report
  90. */
  91. function systemstatus_block( $op='list', $delta=0, $edit=array() ) {
  92. switch ( $op ) {
  93. case 'list': //list the block in the blocks administration page
  94. $blocks[0]['info'] = t( 'Systems Status Reporter' );
  95. $blocks[1]['info'] = t( 'Systems Status Editor' );
  96. return $blocks;
  97. break;
  98. case 'view': //view the block
  99. if ( $delta == 0 ) {
  100. $blocks['subject'] = t( variable_get( 'systemstatus_blocktitle', 'Service Status' ) );
  101. $blocks['content'] = systemstatus_content();
  102. } elseif ( $delta == 1 && user_access( 'update system statuses' ) ) { //user can change systems status
  103. $blocks['subject'] = t( 'Edit Service Status' );
  104. $blocks['content'] = drupal_get_form( 'systemstatus_serviceform' );
  105. }
  106. return $blocks;
  107. break;
  108. }
  109. }
  110. /**
  111. *Add a form to update system status
  112. */
  113. function systemstatus_serviceform( &$form_state ) {
  114. @ini_set( 'auto_detect_line_endings', true ); //standardize line endings
  115. $systems = explode( PHP_EOL, variable_get( 'systemstatus_systems', NULL ) ); //get a list of systems
  116. $form = array(); //initialize the form
  117. foreach ( $systems as $system ) {
  118. /*
  119. Status
  120. 0 = good
  121. 1 = autocheck bad
  122. 2 = bad
  123. */
  124. $commapos = strstr( $system, ',' ) === false ? false : strpos( $system, ',' ); //find the comma delimiter if present
  125. $sysTitle = $commapos === false ? $system : substr( $system, 0, $commapos ); //get the system title
  126. $sysGenTitle = str_replace( ' ', '_', $sysTitle ); //generate a title that can be used to save status
  127. $statuses = array( 0 => 'Up', 1 => 'Error', 2 => 'Down' );
  128. $form['systemstatus_system_' . $sysGenTitle] = array(
  129. '#type' => 'select',
  130. '#title' => t( $sysTitle ),
  131. '#default_value' => variable_get( 'systemstatus_system_' . $sysGenTitle, 0 ),
  132. '#options' => $statuses,
  133. );
  134. }
  135. $form['submit'] = array(
  136. '#type' => 'submit',
  137. '#value' => t( 'Save' ),
  138. );
  139. return $form;
  140. }
  141. /**
  142. *Save the service form
  143. */
  144. function systemstatus_serviceform_submit( $form, &$form_state ) {
  145. //die( var_dump( $form['#post'] ) );
  146. if ( user_access( 'update system statuses' ) ) {
  147. foreach ( $form['#post'] as $field => $value )
  148. if ( strpos( $field, 'systemstatus_system_' ) !== false ) {
  149. if ( variable_get( $field, '' ) !== $value ) {
  150. variable_set( $field, $value );
  151. $sysname = substr( $field , 20 );
  152. variable_set( 'systemstatus_statustime_' . $sysname, $time );
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. *Display the system status in a block
  159. */
  160. function systemstatus_content() {
  161. @ini_set( 'auto_detect_line_endings', true ); //standardize line endings
  162. drupal_add_css( drupal_get_path( 'module', 'systemstatus' ) . '/css/styles.css' ); //enqueue styles
  163. $systems = explode( PHP_EOL, variable_get( 'systemstatus_systems', NULL ) ); //get a list of systems
  164. $output = '<table class="servicestable">' . PHP_EOL;
  165. foreach ( $systems as $system ) {
  166. /*
  167. Status
  168. 0 = good
  169. 1 = autocheck bad
  170. 2 = bad
  171. */
  172. /* Get Saved Data or easily generated data */
  173. $commapos = strstr( $system, ',' ) === false ? false : strpos( $system, ',' ); //find the comma delimiter if present
  174. $sysTitle = $commapos === false ? $system : substr( $system, 0, $commapos ); //get the system title
  175. $sysGenTitle = str_replace( ' ', '_', $sysTitle ); //generate a title that can be used to save status
  176. $sysStatus = variable_get( 'systemstatus_system_' . $sysGenTitle, 0 ); //get the status if we've saved it
  177. $statusTime = variable_get( 'systemstatus_statustime_' . $sysGenTitle, 0 ); //the time the status was last set manually
  178. /* Regenerate status if needed for auto checks */
  179. if ( $commapos !== false ) {
  180. $sysUrl = $commapos === false ? false : substr( $system, $commapos + 1, strlen( $system ) ); //get the systemurl (if present)
  181. if ( $sysStatus !== 2 ) { //don't check if the status has been set manually to "down"
  182. $autostatus = systemstatus_checkurl( $sysUrl ); //check the site status with CURL
  183. if ( $autostatus === false && $sysStatus === 0 ) { //reset status to auto bad
  184. $sysStatus = 1;
  185. $statusTime = time();
  186. variable_set( 'systemstatus_statustime_' . $sysGenTitle, $statusTime );
  187. variable_set( 'systemstatus_system_' . $sysGenTitle, 1 );
  188. } elseif ( $autostatus === true && $sysStatus === 1 ) { //reset status to good
  189. $sysStatus = 0;
  190. $statusTime = time();
  191. variable_set( 'systemstatus_statustime_' . $sysGenTitle, $statusTime );
  192. variable_set( 'systemstatus_system_' . $sysGenTitle, 0 );
  193. }
  194. }
  195. }
  196. switch ( $sysStatus ) {
  197. case 0:
  198. $statusImg = '/' . drupal_get_path( 'module', 'systemstatus' ) . '/images/up.png';
  199. $statusAlt = 'Service up at ' . date( 'D, F jS, Y \a\t g:i: a', $statusTime );
  200. break;
  201. case 1:
  202. $statusImg = '/' . drupal_get_path( 'module', 'systemstatus' ) . '/images/detecteddown.png';
  203. $statusAlt = 'Service appeared to be down at ' . date( 'D, F jS, Y \a\t g:i: a', $statusTime ) . ' but has not been confirmed.';
  204. break;
  205. case 2:
  206. $statusImg = '/' . drupal_get_path( 'module', 'systemstatus' ) . '/images/down.png';
  207. $statusAlt = 'Service down at ' . date( 'D, F jS, Y \a\t g:i: a', $statusTime );;
  208. break;
  209. }
  210. $output .= '<tr class="servicerow">' . PHP_EOL;
  211. $output .= '<td class="systitle">' . $sysTitle . '</td>' . PHP_EOL;
  212. $output .= '<td class="sysstatus"><img class="sysstatusimg" src="' . $statusImg . '" alt="' . $statusAlt . ' width="20" height="20" style="width: 20px; height: 20px; margin: 5px 0 5px 0" /></td>' . PHP_EOL;
  213. $output .= '</tr>' . PHP_EOL;
  214. }
  215. $output .= '</table>' . PHP_EOL;
  216. return $output;
  217. }
  218. /**
  219. * Check if URL is working
  220. */
  221. function systemstatus_checkurl( $url ) {
  222. $conn = curl_init( trim( $url ) );
  223. curl_setopt( $conn, CURLOPT_NOBODY, true );
  224. curl_setopt( $conn, CURLOPT_FOLLOWLOCATION, true );
  225. curl_exec( $conn );
  226. $response = curl_getinfo( $conn, CURLINFO_HTTP_CODE ); //get the http status code
  227. curl_close( $conn );
  228. return intval( $response ) === 200 ? true : false; //if we get a 200 return true, else false
  229. }