[3551804] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * @file index.php |
---|
| 4 | * @brief OpenGnsys REST API: common routes |
---|
| 5 | * @warning All input and output messages are formatted in JSON. |
---|
| 6 | * @note Some ideas are based on article "How to create REST API for Android app using PHP, Slim and MySQL" by Ravi Tamada, thanx. |
---|
| 7 | * @license GNU GPLv3+ |
---|
| 8 | * @author Ramón M. Gómez, ETSII Univ. Sevilla |
---|
| 9 | * @version 1.1.0 - First version |
---|
| 10 | * @date 2016-11-17 |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | // Auxiliar functions. |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * @brief Compose JSON response. |
---|
| 18 | * @param int status Status code for HTTP response. |
---|
| 19 | * @param array response Response data. |
---|
| 20 | * @return string JSON response. |
---|
| 21 | */ |
---|
| 22 | function jsonResponse($status, $response) { |
---|
| 23 | $app = \Slim\Slim::getInstance(); |
---|
| 24 | // HTTP status code. |
---|
| 25 | $app->status($status); |
---|
| 26 | // Content-type HTTP header. |
---|
| 27 | $app->contentType('application/json'); |
---|
| 28 | // JSON response. |
---|
| 29 | echo json_encode($response); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | // Common routes. |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * @brief Get general server information |
---|
| 36 | * @note Route: /info, Method: GET |
---|
| 37 | * @param no |
---|
| 38 | * @return JSON object with basic server information (version, services, etc.) |
---|
| 39 | */ |
---|
| 40 | $app->get('/info', function() { |
---|
[606a0c7] | 41 | // Reading version file. |
---|
[3551804] | 42 | @list($project, $version, $release) = explode(' ', file_get_contents('/opt/opengnsys/doc/VERSION.txt')); |
---|
[606a0c7] | 43 | $response['project'] = trim($project); |
---|
| 44 | $response['version'] = trim($version); |
---|
| 45 | $response['release'] = trim($release); |
---|
[3551804] | 46 | // Getting actived services. |
---|
| 47 | @$services = parse_ini_file('/etc/default/opengnsys'); |
---|
[606a0c7] | 48 | $response['services'] = Array(); |
---|
| 49 | if (@$services["RUN_OGADMSERVER"] === "yes") { |
---|
| 50 | array_push($response['services'], "server"); |
---|
| 51 | $hasOglive = true; |
---|
| 52 | } |
---|
| 53 | if (@$services["RUN_OGADMREPO"] === "yes") array_push($response['services'], "repository"); |
---|
| 54 | if (@$services["RUN_BTTRACKER"] === "yes") array_push($response['services'], "tracker"); |
---|
| 55 | // Reading installed ogLive information file. |
---|
| 56 | if ($hasOglive === true) { |
---|
| 57 | $data = explode('-', @file_get_contents('/opt/opengnsys/doc/veroglive.txt')); |
---|
| 58 | if ($data[0] === "ogLive") { |
---|
| 59 | array_shift($data); |
---|
| 60 | $response['oglive'] = array(); |
---|
| 61 | $tmp = Array(); |
---|
| 62 | $tmp['distribution'] = trim($data[0]); |
---|
| 63 | array_shift($data); |
---|
| 64 | $tmp['revision'] = trim(end($data)); |
---|
| 65 | array_pop($data); |
---|
| 66 | $tmp['kernel'] = trim(implode('-', $data)); |
---|
| 67 | $tmp['directory'] = "tftpboot/ogclient"; |
---|
| 68 | array_push($response['oglive'], $tmp); |
---|
| 69 | } |
---|
| 70 | } |
---|
[3551804] | 71 | jsonResponse(200, $response); |
---|
| 72 | } |
---|
| 73 | ); |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * @brief Get the server status |
---|
| 77 | * @note Route: /status, Method: GET |
---|
| 78 | * @param no |
---|
| 79 | * @return JSON object with all data collected from server status (RAM, %CPU, etc.). |
---|
| 80 | */ |
---|
| 81 | $app->get('/status', function() { |
---|
| 82 | // Getting memory and CPU information. |
---|
| 83 | exec("awk '$1~/Mem/ {print $2}' /proc/meminfo",$memInfo); |
---|
| 84 | $memInfo = array("total" => $memInfo[0], "used" => $memInfo[1]); |
---|
| 85 | $cpuInfo = exec("awk '$1==\"cpu\" {printf \"%.2f\",($2+$4)*100/($2+$4+$5)}' /proc/stat"); |
---|
| 86 | $cpuModel = exec("awk -F: '$1~/model name/ {print $2}' /proc/cpuinfo"); |
---|
| 87 | $response["memInfo"] = $memInfo; |
---|
| 88 | $response["cpu"] = array("model" => trim($cpuModel), "usage" => $cpuInfo); |
---|
| 89 | jsonResponse(200, $response); |
---|
| 90 | } |
---|
| 91 | ); |
---|
| 92 | |
---|