Commit b6a1f2b2 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

moved functions to a single file

parent f0f64cbc
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
*! -------------------------------------------------------------------------- *! --------------------------------------------------------------------------
*/ */
include 'include/response.php'; include 'include/elphel_functions_include.php';
include 'include/devices.php';
$cmd = "donothing"; $cmd = "donothing";
if (isset($_GET['cmd'])) if (isset($_GET['cmd']))
...@@ -40,26 +39,27 @@ switch($cmd){ ...@@ -40,26 +39,27 @@ switch($cmd){
die(symlink($mountpoint,$symlink)); die(symlink($mountpoint,$symlink));
break; break;
case "free_space": case "free_space":
// results are in GB
// /dev/sda2 is not a mountpoint but a device because it does not have a file system // /dev/sda2 is not a mountpoint but a device because it does not have a file system
$res = 0;
if ($_GET['mountpoint']=="/dev/sda2"){ if ($_GET['mountpoint']=="/dev/sda2"){
//root@elphel393:~# cat /home/root/camogm.disk //root@elphel393:~# cat /home/root/camogm.disk
//Device Start LBA Current LBA End LBA //Device Start LBA Current LBA End LBA
///dev/sda2 195334335 545641716 976768065 ///dev/sda2 195334335 545641716 976768065
if (!is_file($camogmdisk)){ if (!is_file($camogmdisk)){
$devices = get_raw_dev();
foreach($devices as $device=>$size){
//size in MB
if ($device=="/dev/sda2") $res = round($size/1048576,2);
}
}else{ }else{
//read camogm.disk file
$res = 10;
} }
respond_xml("test");
//unpartitioned area
}else{ }else{
if (is_dir($mountpoint)) $res = disk_free_space($mountpoint); if (is_dir($mountpoint)) $res = round(disk_free_space($mountpoint)/1024/1024/1024,2);
else $res = 0;
respond_xml($res);
} }
respond_xml($res);
break; break;
default: default:
print("nothing has been done"); print("nothing has been done");
......
...@@ -21,9 +21,7 @@ PHP_SCRIPTS=i2c.php \ ...@@ -21,9 +21,7 @@ PHP_SCRIPTS=i2c.php \
PHPINCLUDES=i2c_include.php \ PHPINCLUDES=i2c_include.php \
show_source_include.php \ show_source_include.php \
parallel_requests.php \ elphel_functions_include.php
response.php \
devices.php
all: all:
@echo "make all in src" @echo "make all in src"
......
<?php
/*
*!***************************************************************************
*! FILE NAME : devices.php
*! DESCRIPTION: get devices info
*! Copyright (C) 2016 Elphel, Inc.
*! --------------------------------------------------------------------------
*! This program is free software: you can redistribute it and/or modify
*! it under the terms of the GNU General Public License as published by
*! the Free Software Foundation, either version 3 of the License, or
*! (at your option) any later version.
*!
*! This program is distributed in the hope that it will be useful,
*! but WITHOUT ANY WARRANTY; without even the implied warranty of
*! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*! GNU General Public License for more details.
*!
*! You should have received a copy of the GNU General Public License
*! along with this program. If not, see <http://www.gnu.org/licenses/>.
*! --------------------------------------------------------------------------
*/
/** Get a list of suitable partitions. The list will contain SATA devices only and
* will have the following format: "name" => "size_in_blocks".
*/
function get_partitions()
{
$names = array();
$regexp = '/([0-9]+) +(sd[a-z0-9]+$)/';
exec("cat /proc/partitions", $partitions);
// the first two elements of an array are table header and empty line delimiter, skip them
for ($i = 2; $i < count($partitions); $i++) {
// select SATA devices only
if (preg_match($regexp, $partitions[$i], $name) == 1) {
$names[$name[2]] = $name[1];
$j++;
}
}
return $names;
}
/** Get a list of disk devices which have file system and can be mounted. This function
* uses 'blkid' command from busybox.
*/
function get_mnt_dev()
{
$partitions = get_partitions();
$devices = array();
$fs_types = array();
foreach ($partitions as $partition => $size) {
$res = array();
$dev = "/dev/" . $partition;
exec("blkid " . $dev, $res);
if (!empty($res)) {
$devices[$i] = preg_replace('/: +.*/', "", $res[0]);
if (preg_match('/(?<=TYPE=")[a-z0-9]+(?=")/', $res[0], $fs) == 1)
$fs_types[$i] = $fs[0];
else
$fs_types[$i] = "none";
$i++;
}
}
return array("devices" => $devices, "types" => $fs_types);
}
/** Get a list of devices whithout file system which can be used for raw disk storage from camogm. */
function get_raw_dev()
{
$j = 0;
$ret = get_mnt_dev();
$devices = $ret["devices"];
$types = $ret["types"];
$names = get_partitions();
// filter out partitions with file system
$i = 0;
$raw_devices = array();
foreach ($names as $name => $size) {
$found = false;
foreach ($devices as $device) {
if (strpos($device, $name) !== false)
$found = true;
}
if ($found === false) {
// current partition is not found in the blkid list, add it to raw devices
$raw_devices["/dev/" . $name] = $size;
$i++;
}
}
//special case
if (count($raw_devices)>1) {
foreach($raw_devices as $k=>$v){
if (preg_match('/sd[a-z][0-9]/',$k)==0) {
unset($raw_devices[$k]);
}
}
}
return $raw_devices;
}
?>
...@@ -20,6 +20,113 @@ ...@@ -20,6 +20,113 @@
*! -----------------------------------------------------------------------------** *! -----------------------------------------------------------------------------**
*/ */
/*
* Contents:
* * OTHER
* * LOG AND XML RESPONSE
* * PARALLEL HTTP REQUESTS
* * DEVICES
*/
// OTHER
function get_uptime(){
exec('cat /proc/uptime',$output,$retval);
return floatval(explode(" ",trim($output[0]))[0]);
}
// LOG AND XML RESPONSE
function colorize($string, $color, $bold) {
$color = strtoupper($color);
$attr = array();
switch ($color) {
case 'RED': $attr[]='31'; break;
case'GREEN': $attr[]='32'; break;
case 'YELLOW': $attr[]='33'; break;
case 'BLUE': $attr[]='34'; break;
case 'MAGENTA': $attr[]='35'; break;
case 'CYAN': $attr[]='36'; break;
case 'GRAY': $attr[]='37'; break;
}
if ($bold) $attr[] = '1';
return sprintf("\x1b[%sm%s\x1b[0m",implode(';',$attr), $string);
}
function log_open(){
$GLOBALS['logFile'] = fopen ( $GLOBALS['logFilePath'], "a" );
}
/** Log message and optionally print to console */
function log_msg($msg, ///< message to print
$mode = -1) ///< -1 - print only short messages, 0 - never print, 1 - always print, 2 print in bold red (error), 3 - bold white, 4 - bold yellow (warning)
{
// do not output log when in HTTP request mode
$ut=get_uptime();
if (($mode != 0) && !array_key_exists ('REQUEST_METHOD', $_SERVER) && (($mode > 0) || (strlen ($msg) < $GLOBALS['LOG_MAX_ECHO']))) {
switch ($mode) {
case 2:
$emsg = colorize($msg,'RED',1); // bold red
break;
case 3:
$emsg = colorize($msg,'',1); // bold white
break;
case 4 :
$emsg = colorize($msg, 'YELLOW', 1); // bold white
break;
default:
$emsg = $msg;
}
printf(colorize(sprintf("[%8.2f] autocampars: ",$ut),"GREEN",0).$emsg."\n");
}
fwrite ($GLOBALS['logFile'], sprintf("%08.2f autocampars: %s\n",$ut,$msg)); // date ("F j, Y, G:i:s")
}
function log_error($msg) {
log_msg ($msg, 2);
log_close ();
exit (1);
}
function log_close() {
log_msg ("Log file saved as " . $GLOBALS['logFilePath'], 3);
log_msg ("----------------------------------------------", 0);
fclose ($GLOBALS['logFile']);
unset ($GLOBALS['logFile']); // to catch errors
}
/** Closes log file, optianally responxds with XML (if in HTTP mode), exits with 0/1 */
function respond_xml($result,$error=null,$color_mode = 3){ // default white bold
if (array_key_exists('REQUEST_METHOD',$_SERVER)){
$xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><Document/>");
if ($result !== ""){ //"" will not be loged/output
if (is_string($result) && ((count($result)==0) || ($result[0] != '"'))){
$result = '"'.$result.'"';
}
$xml->addChild ('result',$result);
}
if ($error){
$xml->addChild ('error','"'.$error.'"');
}
$rslt=$xml->asXML();
header("Content-Type: text/xml");
header("Content-Length: ".strlen($rslt)."\n");
header("Pragma: no-cache\n");
printf($rslt);
}
if (isset($GLOBALS['logFile'])){
if ($result !== ""){ //"" will not be loged/output
log_msg(''.$result,$color_mode);
}
if ($error){
log_error($error);
} else {
log_close();
exit (0);
}
}
}
// PARALLEL HTTP REQUESTS
// Using parallel requests, PHP has to be configured '--with-curl=' (and libcurl should be installed) // Using parallel requests, PHP has to be configured '--with-curl=' (and libcurl should be installed)
function curl_multi_start($urls) { function curl_multi_start($urls) {
// numprime is needed to actually send the request and remote starts executing it // numprime is needed to actually send the request and remote starts executing it
...@@ -87,4 +194,89 @@ function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false) { ...@@ -87,4 +194,89 @@ function curl_multi_finish($data, $use_xml=true, $ntry=0, $echo = false) {
return $results; return $results;
} }
// DEVICES
/** Get a list of suitable partitions. The list will contain SATA devices only and
* will have the following format: "name" => "size_in_blocks".
*/
function get_partitions()
{
$names = array();
$regexp = '/([0-9]+) +(sd[a-z0-9]+$)/';
exec("cat /proc/partitions", $partitions);
// the first two elements of an array are table header and empty line delimiter, skip them
for ($i = 2; $i < count($partitions); $i++) {
// select SATA devices only
if (preg_match($regexp, $partitions[$i], $name) == 1) {
$names[$name[2]] = $name[1];
$j++;
}
}
return $names;
}
/** Get a list of disk devices which have file system and can be mounted. This function
* uses 'blkid' command from busybox.
*/
function get_mnt_dev()
{
$partitions = get_partitions();
$devices = array();
$fs_types = array();
foreach ($partitions as $partition => $size) {
$res = array();
$dev = "/dev/" . $partition;
exec("blkid " . $dev, $res);
if (!empty($res)) {
$devices[$i] = preg_replace('/: +.*/', "", $res[0]);
if (preg_match('/(?<=TYPE=")[a-z0-9]+(?=")/', $res[0], $fs) == 1)
$fs_types[$i] = $fs[0];
else
$fs_types[$i] = "none";
$i++;
}
}
return array("devices" => $devices, "types" => $fs_types);
}
/** Get a list of devices whithout file system which can be used for raw disk storage from camogm. */
function get_raw_dev()
{
$j = 0;
$ret = get_mnt_dev();
$devices = $ret["devices"];
$types = $ret["types"];
$names = get_partitions();
// filter out partitions with file system
$i = 0;
$raw_devices = array();
foreach ($names as $name => $size) {
$found = false;
foreach ($devices as $device) {
if (strpos($device, $name) !== false)
$found = true;
}
if ($found === false) {
// current partition is not found in the blkid list, add it to raw devices
$raw_devices["/dev/" . $name] = $size;
$i++;
}
}
//special case
if (count($raw_devices)>1) {
foreach($raw_devices as $k=>$v){
if (preg_match('/sd[a-z][0-9]/',$k)==0) {
unset($raw_devices[$k]);
}
}
}
return $raw_devices;
}
?> ?>
<?php
/*
*!***************************************************************************
*! FILE NAME : include_response.php
*! DESCRIPTION: http requests response functions
*! Copyright (C) 2016 Elphel, Inc.
*! --------------------------------------------------------------------------
*! This program is free software: you can redistribute it and/or modify
*! it under the terms of the GNU General Public License as published by
*! the Free Software Foundation, either version 3 of the License, or
*! (at your option) any later version.
*!
*! This program is distributed in the hope that it will be useful,
*! but WITHOUT ANY WARRANTY; without even the implied warranty of
*! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*! GNU General Public License for more details.
*!
*! You should have received a copy of the GNU General Public License
*! along with this program. If not, see <http://www.gnu.org/licenses/>.
*! --------------------------------------------------------------------------
*/
function get_uptime(){
exec('cat /proc/uptime',$output,$retval);
return floatval(explode(" ",trim($output[0]))[0]);
}
//printf("%08.2f\n",$f);
function log_open(){
$GLOBALS['logFile'] = fopen ( $GLOBALS['logFilePath'], "a" );
}
/** Log message and optionally print to console */
function log_msg($msg, ///< message to print
$mode = -1) ///< -1 - print only short messages, 0 - never print, 1 - always print, 2 print in bold red (error), 3 - bold white, 4 - bold yellow (warning)
{
// do not output log when in HTTP request mode
$ut=get_uptime();
if (($mode != 0) && !array_key_exists ('REQUEST_METHOD', $_SERVER) && (($mode > 0) || (strlen ($msg) < $GLOBALS['LOG_MAX_ECHO']))) {
switch ($mode) {
case 2:
$emsg = colorize($msg,'RED',1); // bold red
break;
case 3:
$emsg = colorize($msg,'',1); // bold white
break;
case 4 :
$emsg = colorize($msg, 'YELLOW', 1); // bold white
break;
default:
$emsg = $msg;
}
printf(colorize(sprintf("[%8.2f] autocampars: ",$ut),"GREEN",0).$emsg."\n");
}
fwrite ($GLOBALS['logFile'], sprintf("%08.2f autocampars: %s\n",$ut,$msg)); // date ("F j, Y, G:i:s")
}
function log_error($msg) {
log_msg ($msg, 2);
log_close ();
exit (1);
}
function log_close() {
log_msg ("Log file saved as " . $GLOBALS['logFilePath'], 3);
log_msg ("----------------------------------------------", 0);
fclose ($GLOBALS['logFile']);
unset ($GLOBALS['logFile']); // to catch errors
}
/** Closes log file, optianally responxds with XML (if in HTTP mode), exits with 0/1 */
function respond_xml($result,$error=null,$color_mode = 3){ // default white bold
if (array_key_exists('REQUEST_METHOD',$_SERVER)){
$xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><Document/>");
if ($result !== ""){ //"" will not be loged/output
if (is_string($result) && ((count($result)==0) || ($result[0] != '"'))){
$result = '"'.$result.'"';
}
$xml->addChild ('result',$result);
}
if ($error){
$xml->addChild ('error','"'.$error.'"');
}
$rslt=$xml->asXML();
header("Content-Type: text/xml");
header("Content-Length: ".strlen($rslt)."\n");
header("Pragma: no-cache\n");
printf($rslt);
}
if (isset($GLOBALS['logFile'])){
if ($result !== ""){ //"" will not be loged/output
log_msg(''.$result,$color_mode);
}
if ($error){
log_error($error);
} else {
log_close();
exit (0);
}
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment