You are here

function varnish_get_status in Varnish 8

Same name and namespace in other branches
  1. 6 varnish.module \varnish_get_status()
  2. 7 varnish.module \varnish_get_status()

Get the status (up/down) of each of the varnish servers.

Return value

An array of server statuses, keyed by varnish terminal addresses. The status will be a numeric constant, either:

2 calls to varnish_get_status()
VarnishAdminForm::buildForm in src/Form/VarnishAdminForm.php
Form constructor.
varnish_requirements in ./varnish.install
Implements hook_requirements().

File

./varnish.module, line 138
varnish.module

Code

function varnish_get_status() {
  $config = \Drupal::config('varnish.settings');

  // use a static-cache so this can be called repeatedly without incurring
  // socket-connects for each call.
  static $results = NULL;
  if (is_null($results)) {
    $results = [];
    $status = _varnish_terminal_run([
      'status',
    ]);
    $terminals = explode(' ', $config
      ->get('varnish_control_terminal'));
    foreach ($terminals as $terminal) {
      if ($status[$terminal] === VARNISH_SERVER_STATUS_AUTHENTICATION_FAILURE) {
        $results[$terminal] = VARNISH_SERVER_STATUS_AUTHENTICATION_FAILURE;
      }
      elseif ($status[$terminal] === FALSE) {
        $results[$terminal] = VARNISH_SERVER_STATUS_DOWN;
      }
      else {
        $stat = $status[$terminal];
        $results[$terminal] = $stat['status']['code'] == 200 ? VARNISH_SERVER_STATUS_UP : VARNISH_SERVER_STATUS_DOWN;
      }
    }
  }
  return $results;
}