initial code comit

This commit is contained in:
2018-11-22 21:02:44 +03:00
parent dda0488d4b
commit 801bbd37b7
117 changed files with 7612 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
<?php
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
require_once('../settings.php');
$files = array( 'index.html' => 440,
'LICENSE' => 440,
'css' => 550,
'css/core.css' => 440,
'css/core.min.css' => 440,
'gfx' => 550,
'gfx/bg.jpg' => 440,
'gfx/menu.png' => 440,
'gfx/buttons' => 550,
'gfx/buttons/delete.png' => 440,
'gfx/distros' => 550,
'gfx/distros/apple.png' => 440,
'gfx/distros/arch.png' => 440,
'gfx/distros/bsd.png' => 440,
'gfx/distros/debian.png' => 440,
'gfx/distros/fedora.png' => 440,
'gfx/distros/linux.png' => 440,
'gfx/distros/opensuse.png' => 440,
'gfx/distros/ubuntu.png' => 440,
'gfx/distros/unknown.png' => 440,
'gfx/flags' => 550,
'gfx/flags/russian-16x16.png' => 440,
'gfx/flags/uk-16x16.png' => 440,
'gfx/icons' => 550,
'gfx/icons/about.png' => 440,
'gfx/icons/help.png' => 440,
'gfx/icons/loading.gif' => 440,
'gfx/icons/network.png' => 440,
'gfx/icons/server.png' => 440,
'gfx/icons/settings.png' => 440,
'gfx/icons/samba.png' => 440,
'gfx/icons/storage.png' => 440,
'gfx/icons/sysinfo.png' => 440,
'js' => 550,
'js/core.js' => 440,
'js/smoothie.js' => 440,
'system' => 550,
'system/settings.php' => 440,
'system/action' => 550,
'system/action/index.php' => 440,
'system/apps' => 550,
'system/apps/apps.json' => 640,
'system/apps/cai_downloader' => 550,
'system/apps/cai_downloader/app.json' => 440,
'system/apps/cai_downloader/icon.png' => 440,
'system/apps/cai_downloader/index.html' => 440,
'system/apps/cai_downloader/css' => 550,
'system/apps/cai_downloader/css/downloader.css' => 440,
'system/apps/cai_downloader/exec' => 550,
'system/apps/cai_downloader/exec/index.php' => 440,
'system/apps/cai_downloader/exec/scripts' => 550,
'system/apps/cai_downloader/exec/scripts/app.sh' => 550,
'system/apps/cai_downloader/js' => 550,
'system/apps/cai_downloader/js/downloader.js' => 440,
'system/apps/cai_downloader/locale' => 550,
'system/apps/cai_downloader/locale/en.json' => 440,
'system/apps/cai_downloader/locale/ru.json' => 440,
'system/apps/nextcloud' => 550,
'system/apps/nextcloud/app.json' => 440,
'system/apps/nextcloud/icon.png' => 440,
'system/apps/openfire' => 550,
'system/apps/openfire/app.json' => 440,
'system/apps/openfire/icon.png' => 440,
'system/apps/owncloud' => 550,
'system/apps/owncloud/app.json' => 440,
'system/apps/owncloud/icon.png' => 440,
'system/apps/phpmyadmin' => 550,
'system/apps/phpmyadmin/app.json' => 440,
'system/apps/phpmyadmin/icon.png' => 440,
'system/apps/phppgadmin' => 550,
'system/apps/phppgadmin/app.json' => 440,
'system/apps/phppgadmin/icon.png' => 440,
'system/apps/plex' => 550,
'system/apps/plex/app.json' => 440,
'system/apps/plex/icon.png' => 440,
'system/apps/rslsync' => 550,
'system/apps/rslsync/app.json' => 440,
'system/apps/rslsync/icon.png' => 440,
'system/apps/transmission' => 550,
'system/apps/transmission/app.json' => 440,
'system/apps/transmission/icon.png' => 440,
'system/classes' => 550,
'system/classes/CAI' => 550,
'system/classes/CAI/CAICP' => 550,
'system/classes/CAI/CAICP/src' => 550,
'system/classes/CAI/CAICP/src/Applications.php' => 440,
'system/classes/CAI/CAICP/src/Localization.php' => 440,
'system/classes/CAI/CAICP/src/PSR4Autoloader.php' => 440,
'system/classes/CAI/CAICP/src/Settings.php' => 440,
'system/help' => 550,
'system/help/index.html' => 440,
'system/help/css' => 550,
'system/help/css/help.css' => 440,
'system/help/js' => 550,
'system/help/js/help.js' => 440,
'system/help/locale' => 550,
'system/help/locale/en' => 550,
'system/help/locale/en/index.html' => 440,
'system/help/locale/ru' => 550,
'system/help/locale/ru/index.html' => 440,
'system/json' => 550,
'system/json/index.php' => 440,
'system/json/settings.json' => 640,
'system/json/version.json' => 440,
'system/json/general_settings' => 550,
'system/json/general_settings/index.php' => 440,
'system/json/locale' => 550,
'system/json/locale/en.json' => 440,
'system/json/locale/ru.json' => 440,
'system/scripts' => 550,
'system/scripts/check_files.php' => 440,
'system/scripts/index.php' => 440,
'system/scripts/json_api_nix.sh' => 550,
'system/scripts/json_transmission.php' => 440,
'system/scripts/json_transmission.sh' => 550,
'system/scripts/smart_temp.sh' => 550,
'system/scripts/trim.awk' => 440,
'system/ui' => 550,
'system/ui/forms' => 550,
'system/ui/forms/settings' => 550,
'system/ui/forms/settings/index.html' => 440,
'system/ui/forms/settings/css' => 550,
'system/ui/forms/settings/css/styles.css' => 440,
'system/ui/forms/settings/js' => 550,
'system/ui/forms/settings/js/settings.js' => 440
);
$script_response = array();
$err_count = 0;
foreach ($files as $key => $value) {
if (!file_exists($cp_root . '/' . $key)) {
$err_count += 1;
$script_response[] = array( 'file' => $key,
'exists' => 0,
'mask_required' => 0,
'mask_actual' => 0);
} else {
$file_owner = posix_getpwuid(fileowner($cp_root . '/' . $key))['name'];
$file_group = posix_getgrgid(filegroup($cp_root . '/' . $key))['name'];
$file_mask = (int)substr(sprintf('%o',
fileperms($cp_root . '/' . $key)
),
-3);
if (($value != $file_mask) ||
($file_group != FILE_GROUP) ||
$file_owner != FILE_OWNER) {
$err_count += 1;
$script_response[] = array( 'file' => $key,
'exists' => 1,
'mask_required' => $value,
'mask_actual' => $file_mask,
'owner_required' => FILE_OWNER,
'owner_actual' => $file_owner,
'group_required' => FILE_GROUP,
'group_actual' => $file_group);
}
}
}
echo html_entity_decode(json_encode($script_response));

30
system/scripts/index.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
$filtered_widget = filter_input(INPUT_GET, 'widget', FILTER_SANITIZE_STRING);
$filtered_param = preg_replace('/[^a-zA-Z0-9]+/',
'',
filter_input(INPUT_GET,
'param',
FILTER_SANITIZE_STRING)
);
if ($filtered_widget !== '') {
if ($filtered_param !== '') {
echo shell_exec(sprintf('./json_api_nix.sh %s %s',
$filtered_widget,
$filtered_param
)
);
} else {
echo shell_exec(sprintf('./json_api_nix.sh %s',
$filtered_widget
)
);
}
} else {
echo '{}';
}

View File

@@ -0,0 +1,938 @@
#!/bin/bash
# MIT License:
#
# Copyright (c) 2016-2018, Alexander I. Chebykin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#################################################################
# CAI CP v.0.9.1
#
# @module : Bash script module
# @author : Alexander I. Chebykin <alex.chebykin@gmail.com>
# @copyright : Copyright (c) 2016-2018 Alexander I. Chebykin
# @version : 0.9.1
# @build date : 2018-08-23
# @license : MIT
# @link : https://github.com/CAI79/CAI-CP
#################################################################
skeleton() {
cmd_arp=`which arp`
cmd_awk=`which awk`
cmd_cat=`which cat`
cmd_date=`which date`
cmd_df=`which df`
cmd_free=`which free`
cmd_grep=`which grep`
cmd_hostname=`which hostname`
cmd_iostat=`which iostat`
cmd_ip=`which ip`
cmd_lsb_release=`which lsb_release`
cmd_lscpu=`which lscpu`
cmd_netstat=`which netstat`
cmd_ps=`which ps`
cmd_sed=`which sed`
cmd_smartctl=`which smartctl`
cmd_smbstatus=`which smbstatus`
cmd_sort=`which sort`
cmd_ss=`which ss`
cmd_uname=`which uname`
cmd_w=`which w`
result=$()
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
_print_parsed() {
while read data; do
# echo -n "$data" | sed -r "s/\"/\\\\\"/g" | tr -d "\n";
echo -n "$data" | tr -d "\n";
done;
}
#########################
# Common info functions #
#########################
cpu() {
cmd_awk=`which awk`
cmd_cat=`which cat`
result=$({ $cmd_cat /proc/stat; sleep "1"; $cmd_cat /proc/stat; } \
| $cmd_awk '/^cpu* / {usr=$2-usr; sys=$4-sys; idle=$5-idle; iow=$6-iow} END \
{total=usr+sys+idle+iow; printf "%.2f\n", (total-idle)*100/total}' \
| $cmd_awk -F " " '{print "{\n \"cpu\": " $1 "\n}"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# CPU(s) count
cpu_count() {
cmd_awk=`which awk`
cmd_grep=`which grep`
cmd_lscpu=`which lscpu`
result=$($cmd_lscpu \
| $cmd_grep '^CPU(s):' \
| $cmd_awk '{ print "{\n\t\"cpu(s)\": " $2 "\n}" }')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# CPU(s) load information
cpu_load() {
cmd_awk=`which awk`
cmd_cat=`which cat`
cmd_grep=`which grep`
cmd_lscpu=`which lscpu`
cpus=$($cmd_lscpu \
| $cmd_grep '^CPU(s):' \
| $cmd_awk '{ print $2 }')
i=0
while [ $i -lt $cpus ]
do
if [ "$i" -ne 0 ]; then
result="$result,"
fi
result+=$(
{ $cmd_cat /proc/stat; sleep "1"; $cmd_cat /proc/stat; } \
| $cmd_awk '/^cpu'$i' / {usr=$2-usr; sys=$4-sys; idle=$5-idle; iow=$6-iow} END \
{total=usr+sys+idle+iow; printf "%.2f\n", (total-idle)*100/total}' \
| $cmd_awk -F " " '{print "\n \"cpu'$i'\": " int($1) "\n"}'
)
(( i++ ))
done
echo "{$result}" | _print_parsed
}
# CPU information
cpu_info() {
cmd_awk=`which awk`
cmd_lscpu=`which lscpu`
result=$($cmd_lscpu \
| $cmd_awk -F: '{print "\""$1"\": \""$2"\"," }'\
)
echo "[{" ${result%?} "}]" | _print_parsed
}
# General OS information
general_info() {
cmd_awk=`which awk`
cmd_cat=`which cat`
cmd_date=`which date`
cmd_hostname=`which hostname`
cmd_lsb_release=`which lsb_release`
cmd_sed=`which sed`
cmd_uname=`which uname`
function displaytime {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
[[ $D > 0 ]] && printf '%d days ' $D
[[ $H > 0 ]] && printf '%d hours ' $H
[[ $M > 0 ]] && printf '%d minutes ' $M
[[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and '
printf '%d seconds\n' $S
}
lsbRelease=$($cmd_lsb_release -ds | $cmd_sed -e 's/^"//' -e 's/"$//')
uname=$($cmd_uname -r | $cmd_sed -e 's/^"//' -e 's/"$//')
os=`echo $lsbRelease $uname`
hostname=$($cmd_hostname)
uptime_seconds=$($cmd_cat /proc/uptime | $cmd_awk '{print $1}')
server_time=$($cmd_date)
echo "[{ \"OS\": \"$os\", \
\"Hostname\": \"$hostname\", \
\"Uptime\": \" $(displaytime ${uptime_seconds%.*}) \", \
\"Server Time\": \"$server_time\" }]" \
| _print_parsed
}
# OS distriboutive info
os_distr() {
cmd_lsb_release=`which lsb_release`
cmd_sed=`which sed`
cmd_uname=`which uname`
lsbRelease=$($cmd_lsb_release -ds | $cmd_sed -e 's/^"//' -e 's/"$//')
osFamily=$($cmd_uname)
echo "{ \"Distr\": \"$lsbRelease\", \
\"Family\": \"$osFamily\" }" \
| _print_parsed
}
# Running processes list
processes() {
cmd_awk=`which awk`
cmd_ps=`which ps`
cmd_sed=`which sed`
cmd_sort=`which sort`
result=$($cmd_ps -eafw \
| $cmd_sed -e '1d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"uid\": \"%s\", \
\n\t\t\"pid\": %s, \
\n\t\t\"ppid\": %s, \
\n\t\t\"c\": %s, \
\n\t\t\"stime\": \"%s\", \
\n\t\t\"tty\": \"%s\", \
\n\t\t\"time\": \"%s\", \
\n\t\t\"cmd\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, $8)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Server name
srv_name() {
cmd_awk=`which awk`
cmd_uname=`which uname`
result=$($cmd_uname -n | $cmd_awk '{ print "{\n\t\"server_name\": \"" $1 "\"\n}" }')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
##############################
# File system info functions #
##############################
# File system info
fs() {
cmd_awk=`which awk`
cmd_df=`which df`
cmd_sed=`which sed`
cmd_sort=`which sort`
result=$($cmd_df -h -T \
| $cmd_sed -e '1d' \
| $cmd_sort -k1,1 -k7,7 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"fs\": \"%s\", \
\n\t\t\"type\": \"%s\", \
\n\t\t\"size\": \"%s\", \
\n\t\t\"used\": \"%s\", \
\n\t\t\"free\": \"%s\", \
\n\t\t\"percent\": \"%s\", \
\n\t\t\"mount\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7)} \
END{print "\n]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# HDD S.M.A.R.T. status
hdd_smart() {
cmd_awk=`which awk`
cmd_sed=`which sed`
cmd_smartctl=`which smartctl`
result=$(sudo $cmd_smartctl --scan \
| $cmd_awk '{printf("%s ", $1)} {system("sudo smartctl -H \"" $1 "\" \
| sed -n '5p'")}' \
| $cmd_sed -e '/^$/d' \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"drive\": \"%s\",\n\t\t\"status\": \"%s\"\n\t}", $1, $NF)} \
END \
{print "\n]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# HDD temperature info
hdd_temp() {
cmd_awk=`which awk`
cmd_smartctl=`which smartctl`
path=`pwd`
result=$($cmd_smartctl --scan \
| $cmd_awk '{system("./smart_temp.sh \"" $1 "\"")}' \
| $cmd_awk -F: 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{FS=": "} \
{printf("\t\t\"drive\": \"%s\", \
\n\t\t\"model\": \"%s\", \
\n\t\t\"temperature\": \"%s\"\n\t}", $1, $2, $3)} \
END\
{print "\n]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# IO statistics
io_stat() {
cmd_awk=`which awk`
cmd_iostat=`which iostat`
cmd_sed=`which sed`
cmd_sort=`which sort`
result=$($cmd_iostat \
| $cmd_sed -e '1,6d;/^$/d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"device\": \"%s\", \
\n\t\t\"tps\": %s, \
\n\t\t\"kB_read/s\": %s, \
\n\t\t\"kB_wrtn/s\": %s, \
\n\t\t\"kB_read\": %s, \
\n\t\t\"kB_wrtn\": %s\n\t}", $1, $2, $3, $4, $5, $6)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
#########################
# Memory info functions #
#########################
# General memory information
mem_info() {
cmd_awk=`which awk`
cmd_cat=`which cat`
cmd_sed=`which sed`
result=$($cmd_cat /proc/meminfo \
| $cmd_awk -F: 'BEGIN {print "{"} {print "\"" $1 "\": \"" $2 "\"," } END {print "}"}' \
| $cmd_sed 'N;$s/,\n/\n/;P;D')
echo "[" ${result%?} "}]" | _print_parsed
}
# Memory usage information
#
# Based on Linux Dash codebase: https://github.com/afaqurk/linux-dash
#
# References:
# Calculations: http://zcentric.com/2012/05/29/mapping-procmeminfo-to-output-of-free-command/
# Fields: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
mem() {
cmd_awk=`which awk`
cmd_cat=`which cat`
cmd_grep=`which grep`
memInfoFile="/proc/meminfo"
memInfo=`$cmd_cat $memInfoFile | $cmd_grep 'MemTotal\|MemFree\|Buffers\|Cached'`
echo $memInfo \
| $cmd_awk '{print "{ \"total\": " ($2/1024) ", \"used\": " ( ($2-($5+$8+$11))/1024 ) " }" }' \
| _print_parsed
}
# Swap memory info
swap() {
cmd_awk=`which awk`
cmd_free=`which free`
cmd_sed=`which sed`
# result=$($cmd_free -m \
# | $cmd_sed -e '1d' -e '2d' -e '3d' \
# | $cmd_awk 'BEGIN \
# {print "{"} \
# {if (NR > 1) print ","} \
# {printf("\n\t\"total\": %s, \n\t\"used\": %s", $2, $3)} \
# END{print "\n}"}')
result=$($cmd_free -m \
| $cmd_sed '/Swap:/!d' \
| $cmd_awk 'BEGIN \
{print "{"} \
{if (NR > 1) print ","} \
{printf("\n\t\"total\": %s, \n\t\"used\": %s", $2, $3)} \
END{print "\n}"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
##########################
# Network info functions #
##########################
# ARP cache table
#
# Based on Linux Dash codebase: https://github.com/afaqurk/linux-dash
arp_cache() {
cmd_awk=`which awk`
cmd_sed=`which sed`
if hash arp 2>/dev/null; then cmd_arp=`which arp`
else cmd_arp="ip neigh"
fi
result=$($cmd_arp | \
$cmd_awk 'BEGIN {print "["} NR>1 \
{if ( NF==6 ) {print "{ \"addr\": \"" $1 "\", " \
"\"hw_type\": \"" $2 "\", " \
"\"hw_addr\": \"" $3 "\", " \
"\"flags\": \"" $4 "\", " \
"\"mask\": \"" $5 "\" , " \
"\"iface\": \"" $6 "\" }, "} \
if ( NF==5 ) {print "{ \"addr\": \"" $1 "\", " \
"\"hw_type\": \"" $2 "\", " \
"\"hw_addr\": \"" $3 "\", " \
"\"flags\": \"" $4 "\", " \
"\"mask\": \"\", " \
"\"iface\": \"" $5 "\" }, "} \
if ( NF==4 ) {print "{ \"addr\": \"" $1 "\", " \
"\"hw_type\": \"" $2 "\", " \
"\"hw_addr\": \"" $3 "\", " \
"\"flags\": \"\", " \
"\"mask\": \"\", " \
"\"iface\": \"" $4 "\" }, "} \
if ( NF==3 ) {print "{ \"addr\": \"" $1 "\", " \
"\"hw_type\": \"\", " \
"\"hw_addr\": \"" $2 "\", " \
"\"flags\": \"\", " \
"\"mask\": \"\", " \
"\"iface\": \"" $3 "\" }, "} \
} \
END {print "]"}' \
| $cmd_sed 'N;$s/},/}/;P;D')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# IP addresses
#
# Based on Linux Dash codebase: https://github.com/afaqurk/linux-dash
ip_addr() {
cmd_awk=`which awk`
cmd_grep=`which grep`
cmd_ifconfig=`which ifconfig`
cmd_tr=`which tr`
cmd_dig=`which dig`
externalIp=`$cmd_dig +short myip.opendns.com @resolver1.opendns.com`
$cmd_ifconfig \
| $cmd_grep -B1 "inet addr" \
| $cmd_awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' \
| $cmd_awk -v exIp="$externalIp" -F: 'BEGIN {print "["} { print "{ \"interface\": \"" $1 "\", \"ip\": \"" $3 "\" },"} END {print "{ \"interface\": \"external\", \"ip\": \""exIp"\" } ]"}' \
| $cmd_tr -d '\r\n'
}
# Network bandwith
net_band() {
cmd_awk=`which awk`
cmd_cat=`which cat`
cmd_sed=`which sed`
result=$($cmd_cat /proc/net/dev \
| $cmd_awk 'BEGIN\
{print "["} NR>2 \
{print "{ \"interface\": \"" $1 "\"," " \"tx\": " $2 "," " \"rx\": " $10 " }," } \
END\
{print "]"}' \
| $cmd_sed 'N;$s/,\n/\n/;P;D')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Network connections
net_conn() {
cmd_awk=`which awk`
cmd_sed=`which sed`
cmd_sort=`which sort`
cmd_ss=`which ss`
result=$($cmd_ss \
| $cmd_sed -e '1d;/u_str/d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"netid\": \"%s\", \
\n\t\t\"state\": \"%s\", \
\n\t\t\"rcvq\": %s, \
\n\t\t\"sendq\": %s, \
\n\t\t\"local\": \"%s\", \
\n\t\t\"peer\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6)} \
END{print "\n]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Download transfer rate
#
# Based on Linux Dash codebase: https://github.com/afaqurk/linux-dash
net_download_transfer_rate() {
files=(/sys/class/net/*)
pos=$(( ${#files[*]} - 1 ))
last=${files[$pos]}
json_output="{"
for interface in "${files[@]}"
do
basename=$(basename "$interface")
# find the number of bytes transfered for this interface
in1=$(cat /sys/class/net/"$basename"/statistics/rx_bytes)
# wait a second
sleep 1
# check same interface again
in2=$(cat /sys/class/net/"$basename"/statistics/rx_bytes)
# get the difference (transfer rate)
in_bytes=$((in2 - in1))
# convert transfer rate to KB
in_kbytes=$((in_bytes / 1024))
# convert transfer rate to KB
json_output="$json_output \"$basename\": $in_kbytes"
# if it is not the last line
if [[ ! $interface == $last ]]
then
# add a comma to the line (JSON formatting)
json_output="$json_output,"
fi
done
# close the JSON object & print to screen
echo "$json_output}" | _print_parsed
}
# Upload transfer rate
#
# Based on Linux Dash codebase: https://github.com/afaqurk/linux-dash
net_upload_transfer_rate() {
files=(/sys/class/net/*)
pos=$(( ${#files[*]} - 1 ))
last=${files[$pos]}
json_output="{"
for interface in "${files[@]}"
do
basename=$(basename "$interface")
# find the number of bytes transfered for this interface
out1=$(cat /sys/class/net/"$basename"/statistics/tx_bytes)
# wait a second
sleep 1
# check same interface again
out2=$(cat /sys/class/net/"$basename"/statistics/tx_bytes)
# get the difference (transfer rate)
out_bytes=$((out2 - out1))
# convert transfer rate to KB
out_kbytes=$((out_bytes / 1024))
# convert transfer rate to KB
json_output="$json_output \"$basename\": $out_kbytes"
# if it is not the last line
if [[ ! $interface == $last ]]
then
# add a comma to the line (JSON formatting)
json_output="$json_output,"
fi
done
# close the JSON object & print to screen
echo "$json_output}" | _print_parsed
}
# Online users info
users_online() {
cmd_awk=`which awk`
cmd_netstat=`which netstat`
cmd_sed=`which sed`
cmd_smbstatus=`which smbstatus`
cmd_sort=`which sort`
cmd_w=`which w`
result=$($cmd_w \
| $cmd_sed -e '1,2d' \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"user\": \"%s\", \
\n\t\t\"tty\": \"%s\", \
\n\t\t\"from\": \"%s\", \
\n\t\t\"login\": \"%s\", \
\n\t\t\"idle\": \"%s\", \
\n\t\t\"jcpu\": \"%s\", \
\n\t\t\"pcpu\": \"%s\", \
\n\t\t\"what\": \"%s %s %s\"\n\t}", \
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)} \
END \
{print "\n]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# IP routing table
ip_route() {
if hash netstat 2>/dev/null; then ip_route_netstat
else ip_route_ip
fi
}
# IP routing table (ip)
ip_route_ip() {
cmd_awk=`which awk`
cmd_ip=`which ip`
cmd_sed=`which sed`
result=$($cmd_ip r \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{\"route\": \""} \
{print } \
{print "\"\n\t}"} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# IP routing table (netstat)
ip_route_netstat() {
cmd_awk=`which awk`
cmd_netstat=`which netstat`
cmd_sed=`which sed`
result=$($cmd_netstat -r \
| $cmd_sed -e '1,2d' \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"destination\": \"%s\", \
\n\t\t\"gateway\": \"%s\", \
\n\t\t\"genmask\": \"%s\", \
\n\t\t\"flags\": \"%s\", \
\n\t\t\"mss\": %s, \
\n\t\t\"window\": %s, \
\n\t\t\"irtt\": %s, \
\n\t\t\"iface\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, $8)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Multicast groups
net_multicast() {
cmd_awk=`which awk`
cmd_netstat=`which netstat`
cmd_sed=`which sed`
cmd_sort=`which sort`
result=$($cmd_netstat -g \
| $cmd_sed -e '1,3d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"interface\": \"%s\", \
\n\t\t\"refcnt\": %s, \
\n\t\t\"group\": \"%s\"\n\t}", $1, $2, $3)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Listening sockets
listen_socks() {
if hash netstat 2>/dev/null; then listen_socks_netstat
else listen_socks_ss
fi
}
# Listening sockets (ss)
listen_socks_ss() {
cmd_awk=`which awk`
cmd_ss=`which ss`
cmd_sed=`which sed`
result=$($cmd_ss -ltn \
| $cmd_sed -e '1d' \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"state\": \"%s\", \
\n\t\t\"recv-q\": %s, \
\n\t\t\"send-q\": %s, \
\n\t\t\"local address:port\": \"%s\", \
\n\t\t\"peer addr:port\": \"%s\"\n\t}", $1, $2, $3, $4, $5)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Listening sockets (netstat)
listen_socks_netstat() {
cmd_awk=`which awk`
cmd_netstat=`which netstat`
cmd_sed=`which sed`
result=$($cmd_netstat -l \
| $cmd_sed -n '/tcp/p;/udp/p;/raw/p' \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"proto\": \"%s\", \
\n\t\t\"recv-q\": %s, \
\n\t\t\"send-q\": %s, \
\n\t\t\"localaddr\": \"%s\", \
\n\t\t\"foreignaddr\": \"%s\", \
\n\t\t\"state\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Active unix domain sockets
active_unix_socks() {
if hash netstat 2>/dev/null; then active_unix_socks_netstat
else active_unix_socks_ss
fi
}
# Active unix domain sockets (ss)
active_unix_socks_ss() {
cmd_awk=`which awk`
cmd_ss=`which ss`
cmd_sed=`which sed`
cmd_sort=`which sort`
result=$($cmd_ss -x \
| $cmd_sed -e '1d' \
| $cmd_sort -k6 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"netid\": \"%s\", \
\n\t\t\"state\": \"%s\", \
\n\t\t\"recv-q\": %s, \
\n\t\t\"send-q\": %s, \
\n\t\t\"local address:port\": \"%s:%s\", \
\n\t\t\"peer address:port\": \"%s:%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, $8)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Active unix domain sockets (netstat)
active_unix_socks_netstat() {
cmd_awk=`which awk`
cmd_netstat=`which netstat`
cmd_sed=`which sed`
cmd_sort=`which sort`
result=$($cmd_netstat -l \
| $cmd_sed -n '/unix/p' \
| $cmd_sort -k6 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"proto\": \"%s\", \
\n\t\t\"refcnt\": %s, \
\n\t\t\"flags\": \"%s%s%s\", \
\n\t\t\"type\": \"%s\", \
\n\t\t\"state\": \"%s\", \
\n\t\t\"i-node\": \"%s\", \
\n\t\t\"path\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, $8, $9)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# Check is application installed or not
check_app() {
cmd_awk=`which awk`
result=$(whereis -S /opt -f $1 \
| $cmd_awk -F: '{if(length($2)==0) { installed="false"; } else { installed="true"; } \
print \
"{ \
\"binary\": \""$1"\", \
\"location\": \""$2"\", \
\"installed\": "installed" \
}"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# SMB processes
smb_proc() {
cmd_awk=`which awk`
cmd_sed=`which sed`
cmd_smbstatus=`which smbstatus`
cmd_sort=`which sort`
result=$(sudo $cmd_smbstatus -pf \
| $cmd_sed -e '1,4d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"pid\": %s, \
\n\t\t\"username\": \"%s\", \
\n\t\t\"group\": \"%s\", \
\n\t\t\"machine\": \"%s\", \
\n\t\t\"protocol ver\": \"%s\", \
\n\t\t\"encryption\": \"%s\", \
\n\t\t\"signing\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# SMB shares
smb_shares() {
cmd_awk=`which awk`
cmd_sed=`which sed`
cmd_smbstatus=`which smbstatus`
cmd_sort=`which sort`
result=$(sudo $cmd_smbstatus -Sf \
| $cmd_sed -e '1,3d;/^\s*$/d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{print "\t{"} \
{printf("\t\t\"service\": \"%s\", \
\n\t\t\"pid\": %s, \
\n\t\t\"machine\": \"%s\", \
\n\t\t\"connected at\": \"%s %s %s %s %s %s\", \
\n\t\t\"encryption\": \"%s\", \
\n\t\t\"signing\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
# SMB locked files
smb_locks() {
cmd_awk=`which awk`
cmd_sed=`which sed`
cmd_smbstatus=`which smbstatus`
cmd_sort=`which sort`
result=$(sudo $cmd_smbstatus -Lf \
| $cmd_sed -e '1,3d;/^\s*$/d' \
| $cmd_sort -k1 \
| $cmd_awk 'BEGIN \
{print "["} \
{if (NR > 1) print ","} \
{fname = ""} \
{ftime = ""} \
{for (i = 8; i < NF - 4; i++) fname = fname" "$i} \
{for (i = NF - 4; i < NF; i++) ftime = ftime" "$i} \
{print "\t{"} \
{printf("\t\t\"pid\": %s, \
\n\t\t\"uid\": %s, \
\n\t\t\"deny mode\": \"%s\", \
\n\t\t\"access\": \"%s\", \
\n\t\t\"r/w\": \"%s\", \
\n\t\t\"oplock\": \"%s\", \
\n\t\t\"share path\": \"%s\", \
\n\t\t\"name\": \"%s\", \
\n\t\t\"time\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, fname, ftime)} \
END \
{print "\n\t]"}')
if [ -z "$result" ]; then echo {}
else echo $result | _print_parsed
fi
}
function_name="$1"
parameter="$2"
${function_name} ${parameter}

View File

@@ -0,0 +1,15 @@
<?php
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
require_once('../settings.php');
if (W_TRANSMISSION_ENABLED) {
echo shell_exec('./json_transmission.sh ' . W_TRANSMISSION_HOST . ' '
. W_TRANSMISSION_NAME . ' '
. W_TRANSMISSION_PASS);
} else {
echo '[]';
}

View File

@@ -0,0 +1,2 @@
#! /bin/bash
transmission-remote $1 -n $2:$3 -l | sed '$d' | sed -e '1d' | awk '@include "trim.awk";{{printf("%s;%s;%s;%s;%s;%s;%s;%s;%s;", $1, $2, $3, $4, $5, $6, $7, $8, $9)} {$1=$2=$3=$4=$5=$6=$7=$8=$9=""; print trim($0)}}' | awk -F ";" 'BEGIN{print "["}{if (NR > 1) print ","}{print "\t{"}{printf("\t\t\"ID\": \"%s\",\n\t\t\"Done\": \"%s\",\n\t\t\"Have\": \"%s %s\",\n\t\t\"ETA\": \"%s\",\n\t\t\"Up\": \"%s\",\n\t\t\"Down\": \"%s\",\n\t\t\"Ratio\": \"%s\",\n\t\t\"Status\": \"%s\",\n\t\t\"Name\": \"%s\"\n\t}", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)}END{print "\n]"}'

View File

@@ -0,0 +1,18 @@
#!/bin/bash
cmd_awk=`which awk`
cmd_grep=`which grep`
cmd_hddtemp=`which hddtemp`
cmd_smartctl=`which smartctl`
drvtemp=$(sudo $cmd_hddtemp $1)
hdd=$(echo "$drvtemp" | $cmd_awk -F: '{print $1}')
model=$(echo "$drvtemp" | $cmd_awk -F: '{print $2}')
result=$(sudo $cmd_smartctl -a $1 | $cmd_grep "Current Drive Temperature" | $cmd_awk '{print $4"°"$5}')
if [ -z "$result" ];
then
result=$(echo "$drvtemp" | $cmd_awk -F: '{print $3}')
fi
echo $hdd: $model: $result

3
system/scripts/trim.awk Normal file
View File

@@ -0,0 +1,3 @@
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }