source: admin/WebConsole/rest/common.php @ cf30efc

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since cf30efc was 3551804, checked in by ramon <ramongomez@…>, 8 years ago

#708: Separar funciones y rutas REST comunes; nueva ruta /info con información de versión y servicios activos.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5096 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 2.6 KB
Line 
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 */     
22function 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() {
41      // Getting version info.
42      @list($project, $version, $release) = explode(' ', file_get_contents('/opt/opengnsys/doc/VERSION.txt'));
43      $response["project"] = $project;
44      $response["version"] = $version;
45      $response["release"] = trim($release);
46      // Getting actived services.
47      @$services = parse_ini_file('/etc/default/opengnsys');
48      $response["services"] = Array();
49      if (@$services["RUN_OGADMSERVER"] === "yes")  array_push($response["services"], "server");
50      if (@$services["RUN_OGADMREPO"] === "yes")  array_push($response["services"], "repository");
51      if (@$services["RUN_BTTRACKER"] === "yes")  array_push($response["services"], "tracker");
52      jsonResponse(200, $response);
53   }
54);
55
56/**
57 * @brief    Get the server status
58 * @note     Route: /status, Method: GET
59 * @param    no
60 * @return   JSON object with all data collected from server status (RAM, %CPU, etc.).
61 */
62$app->get('/status', function() {
63      // Getting memory and CPU information.
64      exec("awk '$1~/Mem/ {print $2}' /proc/meminfo",$memInfo);
65      $memInfo = array("total" => $memInfo[0], "used" => $memInfo[1]);
66      $cpuInfo = exec("awk '$1==\"cpu\" {printf \"%.2f\",($2+$4)*100/($2+$4+$5)}' /proc/stat");
67      $cpuModel = exec("awk -F: '$1~/model name/ {print $2}' /proc/cpuinfo");
68      $response["memInfo"] = $memInfo;
69      $response["cpu"] = array("model" => trim($cpuModel), "usage" => $cpuInfo);
70      jsonResponse(200, $response);
71   }
72);
73
Note: See TracBrowser for help on using the repository browser.