上传文件至 ''

1. added test_scandev.php
This commit is contained in:
toby 2023-03-28 20:48:33 +08:00
parent 2a4fac5f3a
commit 6003c049e0
1 changed files with 418 additions and 0 deletions

418
test_scandev.php Normal file
View File

@ -0,0 +1,418 @@
<?php
$ini_path = "/usr/local/etc/frpc.ini";
run($ini_path);
/**
* 将服务的所有设备信息扫描后发送到服务中心;
*/
function run($ini_path)
{
$url = "https://devms.t.educg.com/api/pushDevInfo.do";
$community = "cacti_public";
// $ini_path = "/etc/dev.ini";
//获取要扫描的设备ip。
$list = getDevIp($ini_path);
if (!empty($list)) {
//循环扫描所有设备信息,并发送到服务中心;
foreach ($list as $ip) {
$data = scanDev($ip, $community);
$data["ip"] = $ip;
$res = request_post($url, $data);
}
echo "ok,发送成功!";
} else {
echo "error,无数据!";
}
}
function getDevIp($file_name)
{
$iplist = [];
@$file = fopen($file_name, "r");
if($file){
$i = 0;
//输出文本中所有的行,直到文件结束为止。
while (!feof($file)) {
$item = trim(fgets($file)); //fgets()函数从文件指针中读取一行
//将ip行中的ip提取出来
if (strpos($item, "_ip")) {
$one_item = explode("=", $item);
if (count($one_item) > 0) {
$value = trim($one_item[1]);
if ($value != "127.0.0.1") {
$iplist[] = $value;
}
}
}
$i++;
}
fclose($file);
}
return $iplist;
}
function getIp($arr)
{
if (strpos($arr, "_ip")) {
return $arr;
}
}
/**
* 总扫描;
*/
function scanDev($ip, $community)
{
$cpu = '';
$ram = '';
$cpusort = '';
$uptime = '';
$hostname = '';
$ram_percent = '';
$diskinfo = "";
$rootpercent = "";
$cpuinfo = "";
$tcpcount = "";
$inout = "";
if (!empty($ip) && !empty($community)) {
//获取负载,运行时间,主机名,内存信息;
$baseinfo = get_server_info($ip, $community);
if (count($baseinfo) > 0) {
$cpu = $baseinfo[0];
$ram = $baseinfo[1];
$cpusort = $baseinfo[2];
$uptime = $baseinfo[3];
$hostname = $baseinfo[4];
$ram_percent = $snmpReturn[5];
// $used_ram = $snmpReturn[6];
// $fumv = $snmpReturn[7];
}
//如果uptiem为空说明扫描不到则退出不进行后续扫描
if($uptime==''){
$data['cpu'] = $cpu;
$data['ram'] = $ram;
$data['cpusort'] = $cpusort;
$data['uptime'] = $uptime;
$data['hostname'] = $hostname;
$data['ram_percent'] = $ram_percent;
$data['diskinfo'] = $diskinfo;
$data['inout'] = $inout;
$data['rootpercent'] = $rootpercent;
$data['cpuinfo'] = $cpuinfo;
$data['tcpcount'] = $tcpcount;
return $data;
}
//硬盘信息
$diskinfo = diskInfo($ip, $community);
if (!empty($diskinfo)) {
foreach ($diskinfo as $value) {
if ($value["path"] == "/") {
$rootpercent = $value["diskpercent"];
}
}
}
//记录当前流量;
$currinout = ifinoutInfo($ip, "cacti_public"); //当前累计流量数据
//获取cpu,tcp信息
$cpuinfo = getCpuInfo($ip, $community);
$tcpcount = getTcpInfo($ip, $community);
}
$data['cpu'] = $cpu;
$data['ram'] = $ram;
$data['cpusort'] = $cpusort;
$data['uptime'] = $uptime;
$data['hostname'] = $hostname;
$data['ram_percent'] = $ram_percent;
$data['diskinfo'] = $diskinfo;
$data['inout'] = $currinout;
$data['rootpercent'] = $rootpercent;
$data['cpuinfo'] = $cpuinfo;
$data['tcpcount'] = $tcpcount;
return $data;
}
/**
* 获取负载值;
*/
function get_server_info($ip, $community)
{
$ip = trim($ip);
$result = [];
// $oid_cpu_1 = "1.3.6.1.4.1.2021.10.1.3.1"; //cpu1分钟时负载
$oid_cpu_2 = "1.3.6.1.4.1.2021.10.1.3.2"; //cpu5分钟时负载
// $oid_cpu_3 = "1.3.6.1.4.1.2021.10.1.3.3"; //cpu15分钟时负载
$oid_all_ram = "1.3.6.1.4.1.2021.4.5.0"; //Total RAM in machine: 总的内存
// $oid_used_ram = "1.3.6.1.4.1.2021.4.6.0"; //Total RAM in machine: 可用实际总的内存
$oid_mem_free = "1.3.6.1.4.1.2021.4.11.0"; //可用内存memTotalFree
$oid_mem_share = "1.3.6.1.4.1.2021.4.13.0"; //可用内存memTotalFree
$oid_mem_Buffer = "1.3.6.1.4.1.2021.4.14.0"; //可用内存memTotalFree
$oid_mem_cached = "1.3.6.1.4.1.2021.4.15.0"; //可用内存memTotalFree
$oid_mem_aswap = "1.3.6.1.4.1.2021.4.4.0";//可用memAvailSwap;free应该-这个值才是真是free
$oid_sysname = "SNMPv2-MIB::sysName.0"; //主机名;
// $oid_uptime = "1.3.6.1.2.1.1.3.0";//snmp运行时间
$oid_uptime = "HOST-RESOURCES-MIB::hrSystemUptime.0"; //系统开机时间;
try {
$session = new SNMP(SNMP::VERSION_2C, $ip, $community, 1000000, 2);
@$rep = $session->get(array($oid_cpu_2, $oid_mem_aswap,$oid_all_ram,$oid_mem_share, $oid_mem_cached,$oid_mem_Buffer,$oid_mem_free, $oid_uptime, $oid_sysname), true); //连不上会有超时报警
if ($rep === false) {
$session->close();
return $result;
} else {
$cpu_v = 0;
$cpu_sort = 0;
if (isset($rep[$oid_cpu_2])) {
$cpu_2 = $rep[$oid_cpu_2];
$cpu_2_v = explode(":", $cpu_2)[1];
$cpu_2_v = floatval($cpu_2_v);
$cpu_v = $cpu_2_v;
$cpu_sort = $cpu_v;
}
$memv = "0";
$memv_percent = "";
$umbv = "0";
$fumv = "0";
if (isset($rep[$oid_all_ram])) {
$mem = $rep[$oid_all_ram];
$memlist = explode(":", $mem);
$memvalue = $memlist[1];
$memnumb = explode(" ", $memvalue);
$memv_i = intval($memnumb[1] / 1024 / 1024);
$memv = $memv_i;
$used_mem = $rep[$oid_mem_free];//可用
$um = explode(":", $used_mem)[1];
$umb = explode(" ", $um);
$umbv = round($umb[1] / 1024 / 1024,2);
$aswap_mem = $rep[$oid_mem_aswap];//可用
$aswap = explode(":", $aswap_mem)[1];
$aswap = explode(" ", $aswap);
$swapv = round($aswap[1] / 1024 / 1024,2);
$umbv = $umbv-$swapv;//真是free值
$cached_mem = $rep[$oid_mem_cached];//cached
$fum = explode(":", $cached_mem)[1];
$fum = explode(" ", $fum);
$fumv = round($fum[1] / 1024 / 1024,2);
$buffer_mem = $rep[$oid_mem_Buffer];//cached
$buffer = explode(":", $buffer_mem)[1];
$buffer = explode(" ", $buffer);
$bumv = round($buffer[1] / 1024 / 1024,2);
$share_mem = $rep[$oid_mem_share];//cached
$share = explode(":", $share_mem)[1];
$share = explode(" ", $share);
$shmv = round($share[1] / 1024 / 1024,2);
if(($shmv+$fumv+$bumv)>$memv){
$memv_percent = round(abs($memv-$umbv-$fumv-$bumv+$shmv) / $memv * 100, 0) . "%";
}else{
$memv_percent = round(abs($memv-$umbv-$fumv-$bumv) / $memv * 100, 0) . "%";
}
}
$uptime = "";
if (isset($rep[$oid_uptime])) {
$uptime = $rep[$oid_uptime];
$uptime = explode(") ", $uptime);
$uptime = $uptime[1];
}
$hostname = "";
if (isset($rep[$oid_sysname])) {
$hostname = $rep[$oid_sysname];
$hostname = explode(": ", $hostname)[1];
}
$session->close();
$result[] = $cpu_v;
$result[] = $memv; //总内存大小
$result[] = $cpu_sort; //
$result[] = $uptime; //开机时间
$result[] = $hostname; //主机名
$result[] = $memv_percent; //内存使用率
$result[] = $memv-$umbv;//内存使用
$result[] = $fumv;
}
} catch (\Throwable $th) {
//未知异常;
}
return $result;
}
//硬盘信息获取;
function diskInfo($ip, $community)
{
$disklist = [];
try {
@$Index = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrStorageIndex", 1000000, 2);
@$Type = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrStorageType", 1000000, 2);
@$Descr = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrStorageDescr", 1000000, 2);
@$AS = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrStorageSize", 1000000, 2); //簇的的数目
@$US = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrStorageUsed", 1000000, 2); //使用多少,跟总容量相除就是占用率
@$Units = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrStorageAllocationUnits", 1000000, 2); //簇大小
if(!empty($Index)){
for ($i = 0; $i < count($Index); $i++) {
$temp = array();
$index_one = $Index[$i];
$type_one = $Type[$i];
$descr_one = $Descr[$i];
$as_one = $AS[$i];
$us_one = $US[$i];
$unit_one = $Units[$i];
$i_v = explode(": ", $index_one)[1];
$t_v = explode("::", $type_one)[1];
$d_v = explode(": ", $descr_one)[1];
$a_v = explode(": ", $as_one)[1];
$u_v = explode(": ", $us_one)[1];
$unit_v = explode(" ", $unit_one)[1];
if ($t_v == "hrStorageFixedDisk") {
$temp["index"] = $i_v;
$temp["path"] = $d_v;
$temp["allspace"] = round(($a_v / 1024 / 1024 / 1024) * $unit_v, 2) . 'GB';
$temp["usedspace"] = round(($u_v / 1024 / 1024 / 1024) * $unit_v, 2) . 'GB';
if ($a_v != 0) {
$temp["diskpercent"] = round($u_v / $a_v * 100, 2) . '%';
} else {
$temp["diskpercent"] = "";
}
$disklist[] = $temp;
}
}
}
} catch (\Throwable $th) {
return array();
}
return $disklist;
}
/**
* 获取tcp连接数
*/
function getTcpInfo($ip, $community)
{
$result = 0;
@$tcpCurrEstab = snmp2_walk($ip, $community, "TCP-MIB::tcpCurrEstab.0", 1000000, 2);
if($tcpCurrEstab){
$info = $tcpCurrEstab[0];
$result = intval(explode(": ", $info)[1]);
}
return $result;
}
/**
* 获取cpu核心数和使用率
* cpu负载值之和/处理器的个数)%
* hrProcessorLoad 负载值,多个;
*
*/
function getCpuInfo($ip, $community)
{
$result = array();
@$cpu_proc = snmp2_walk($ip, $community, "HOST-RESOURCES-MIB::hrProcessorLoad", 1000000, 2);
if($cpu_proc){
$cpu_count = count($cpu_proc);
$cpu_v = 0;
for ($i = 0; $i < $cpu_count; $i++) {
$cpu_p = $cpu_proc[$i];
$v = explode(": ", $cpu_p)[1];
$cpu_v += $v;
}
$result["count"] = $cpu_count;
$result["usage"] = round($cpu_v / $cpu_count, 2) . '%';
}
// $result["count"] = 1;
// $result["usage"] = '1%';
return $result;
}
/**
* 获取当前流量
*
*/
function ifinoutInfo($ip, $community)
{
@$curr_in = snmp2_walk($ip, $community, "IF-MIB::ifHCInOctets", 1000000, 2);
@$curr_out = snmp2_walk($ip, $community, "IF-MIB::ifHCOutOctets", 1000000, 2);
$list = array();
if($curr_in){
for ($i = 0; $i < count($curr_in); $i++) {
$temp = [];
$in_one = $curr_in[$i];
$out_one = $curr_out[$i];
$i_v = explode(": ", $in_one)[1];
$o_v = explode(": ", $out_one)[1];
$temp["in"] = $i_v;
$temp["out"] = $o_v;
$list[] = $temp;
}
}
return $list;
}
/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
function request_post($postUrl = '', $curlPost = array())
{
if (empty($postUrl) || empty($curlPost)) {
return false;
}
// 初始化curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_NOSIGNAL, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 4000);
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 5000);
// curl_setopt($curl, CURLOPT_TIMEOUT,10);
// post提交方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curlPost));
// 运行curl
$data = curl_exec($curl);
$curl_errno = curl_errno($curl);
curl_close($curl);
if ($curl_errno > 0) {
return ""; //错误返回空;
}
return $data;
}