Example #1
0
 /**
  * @param \BigWhoop\HAProxyAPI\Client\HTTPClient $client
  * @return array
  * @throws Exception
  */
 public function execute(HTTPClient $client)
 {
     $csv = $client->request('/', array('csv'))->getBody();
     $rows = array_map(function ($val) {
         return explode(',', $val);
     }, explode("\n", trim($csv)));
     $fields = array_map(function ($val) {
         return preg_replace('/[^a-z]+/', '', $val);
     }, array_shift($rows));
     $data = array();
     for ($rowIdx = 0, $numRows = count($rows); $rowIdx < $numRows; $rowIdx++) {
         for ($colIdx = 0, $numCols = count($fields); $colIdx < $numCols; $colIdx++) {
             if (!array_key_exists($rowIdx, $data)) {
                 $data[$rowIdx] = new \stdClass();
             }
             $field = $fields[$colIdx];
             if (!empty($field)) {
                 $data[$rowIdx]->{$field} = $rows[$rowIdx][$colIdx];
             }
         }
     }
     switch ($this->getOption('grouping', self::GROUPING_NONE)) {
         case self::GROUPING_NONE:
             return $data;
         case self::GROUPING_BACKEND:
             $sortedData = array();
             foreach ($data as $server) {
                 if (!array_key_exists($server->pxname, $sortedData)) {
                     $sortedData[$server->pxname] = array();
                 }
                 $sortedData[$server->pxname][] = $server;
             }
             foreach ($sortedData as $backend => $servers) {
                 usort($servers, function ($a, $b) {
                     return strnatcasecmp($a->svname, $b->svname);
                 });
                 $sortedData[$backend] = $servers;
             }
             return $sortedData;
         default:
             throw new Exception("Invalid sorting option provided for stats command.");
     }
 }
Example #2
0
 /**
  * @param \BigWhoop\HAProxyAPI\Client\HTTPClient $client
  * @return bool     true if the action made any changes, otherwise false.
  * @throws Exception
  */
 public function execute(HTTPClient $client)
 {
     $server = $this->getOption('server');
     $action = $this->action;
     $backend = $this->getOption('backend');
     // The order of the params is important. For whatever silly reason.
     $response = $client->request('/', array('s' => $server, 'action' => $action, 'b' => $backend), 'post');
     $statusCode = $response->getHeader('http_code', 500);
     $redirectUrl = $response->getHeader('redirect_url', false);
     if ($statusCode === 303) {
         $redirectUrlPath = parse_url($redirectUrl, PHP_URL_PATH);
         if ($redirectUrlPath == '/;st=DONE') {
             return true;
         } elseif ($redirectUrlPath == '/;st=NONE') {
             return false;
         }
     }
     throw new Exception("HAProxy '{$action}' request for '{$backend}/{$server}' failed.");
 }