From 478a6e251361cf00ea18c08a453b0c8caaa9ef6f Mon Sep 17 00:00:00 2001 From: lgq <1015864684@qq.com> Date: 星期六, 12 七月 2025 17:24:15 +0800 Subject: [PATCH] 修改SNMP导出OID表格,增加设备类型列 --- local/bin/export.sh | 4 local/bin/snmp_val.sh | 8 local/bin/snmp_cfg | 0 local/www/sys-snmp-conf.html | 441 +++++++++++++++++++++++++++--------------------- local/www/cgi-bin/snmp/query_oid | 41 ++++ 5 files changed, 298 insertions(+), 196 deletions(-) diff --git a/local/bin/export.sh b/local/bin/export.sh index 51fe1e6..f3104f9 100755 --- a/local/bin/export.sh +++ b/local/bin/export.sh @@ -1,7 +1,7 @@ #!/bin/sh -sqlite3 /work/iot_cfg.db ".headers on " "select d.name as 璁惧鍚嶇О,'1.3.6.1.4.1.27116.'||d.id||'.'||a.passcode as OID, d.id as 璁惧ID, a.passcode as 閫氶亾鍙�, a.name as 鏁版嵁鍚嶇О,a.note as 澶囨敞 from device d left join accuracy a on d.id=a.id where uploadFlag=0" > /tmp/oid_list.csv -sqlite3 /work/iot_cfg.db "SELECT name as 璁惧鍚嶇О, '1.3.6.1.4.1.27116.'||id||'.3' as OID, id as 璁惧ID, 3 as 閫氶亾鍙� , '鐘舵��' as 鏁版嵁鍚嶇О, '0-姝e父;1-鍛婅' as 澶囨敞 FROM ioDevice ;" >> /tmp/oid_list.csv +sqlite3 /work/iot_cfg.db ".headers on " "select d.name as 璁惧鍚嶇О,'1.3.6.1.4.1.27116.'||d.id||'.'||a.passcode as OID, d.id as 璁惧ID, d.type as 璁惧绫诲瀷, a.passcode as 閫氶亾鍙�, a.name as 鏁版嵁鍚嶇О,a.note as 澶囨敞 from device d left join accuracy a on d.id=a.id where uploadFlag=0" > /tmp/oid_list.csv +sqlite3 /work/iot_cfg.db "SELECT name as 璁惧鍚嶇О, '1.3.6.1.4.1.27116.'||id||'.3' as OID, id as 璁惧ID, type as 璁惧绫诲瀷, 3 as 閫氶亾鍙� , '鐘舵��' as 鏁版嵁鍚嶇О, '0-姝e父;1-鍛婅' as 澶囨敞 FROM ioDevice ;" >> /tmp/oid_list.csv sed -i 's/|/,/g' /tmp/oid_list.csv diff --git a/local/bin/snmp_cfg b/local/bin/snmp_cfg index 195883a..165cd67 100755 --- a/local/bin/snmp_cfg +++ b/local/bin/snmp_cfg Binary files differ diff --git a/local/bin/snmp_val.sh b/local/bin/snmp_val.sh index bf4378e..837dfba 100755 --- a/local/bin/snmp_val.sh +++ b/local/bin/snmp_val.sh @@ -5,7 +5,7 @@ # 鑾峰彇浼犲叆鐨� OID OID="$1" -jsoner=/usr/sbin/jsoner +jsoner=./jsoner #echo "鑴氭湰琚皟鐢�, OID: $OID" chn=${OID##*.} @@ -23,11 +23,11 @@ if [ $ret -eq 0 ] then - RESPONSE="{\"type\": \"OctetString\", \"value\": \"$val\" }" + RESPONSE="{\"dataType\": \"OctetString\", \"value\": \"$val\" }" echo "$RESPONSE" else # 榛樿杩斿洖 - RESPONSE="{\"type\": \"OctetString\", \"value\": \"noData\"}" + RESPONSE="{\"dataType\": \"OctetString\", \"value\": \"noData\"}" echo "$RESPONSE" -fi +fi \ No newline at end of file diff --git a/local/www/cgi-bin/snmp/query_oid b/local/www/cgi-bin/snmp/query_oid new file mode 100755 index 0000000..a3e855f --- /dev/null +++ b/local/www/cgi-bin/snmp/query_oid @@ -0,0 +1,41 @@ +#!/bin/sh +#SNMP妯$粍鐘舵�佹煡璇� +export PATH=/sbin:/usr/sbin:$PATH +echo -en "Content-type: text/html; charset=utf-8\n\n" +#***************************************************** +source ../bin/env.sh +db="../bin/db" + +file="/tmp/oid_list.csv" + +/usr/local/bin/export.sh >> /dev/null + +awk -F ',' ' +BEGIN { + print "[" +} +NR > 1 && $0 != "" { # 璺宠繃棣栬鏍囬锛堝鏈夛級鍜岀┖琛� + printf " {\n" + printf " \"device\": \"%s\",\n", $1 + printf " \"oid\": \"%s\",\n", $2 + printf " \"index1\": %d,\n", $3 + printf " \"type\": %d,\n", $4 + printf " \"index2\": %d,\n", $5 + printf " \"param\": \"%s\"", $6 + # 澶勭悊鍗曚綅瀛楁锛堢6鍒楋級 + if (NF >= 7 && $7 != "") { + printf ",\n \"unit\": \"%s\"\n", $7 + } else { + printf "\n" + } + # 鍒ゆ柇鏄惁涓烘渶鍚庝竴琛� + if (NR == last_line) { + printf " }\n" + } else { + printf " },\n" + } +} +END { + print "]" +} +' last_line=$(wc -l < $file) $file diff --git a/local/www/sys-snmp-conf.html b/local/www/sys-snmp-conf.html index c79ccbe..2da61ef 100644 --- a/local/www/sys-snmp-conf.html +++ b/local/www/sys-snmp-conf.html @@ -1,190 +1,251 @@ -<!DOCTYPE html> -<html> -<head> -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<meta name="viewport" - content="width=device-width, initial-scale=1, maximum-scale=1"> -<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> -<meta name="renderer" content="webkit"> -<title>鏅鸿兘杩愮淮绠$悊绯荤粺-SNMP</title> - - <link rel="stylesheet" type="text/css" href="plugins/layui/css/layui.css" /> - <link rel="stylesheet" type="text/css" href="css/page/device-control.css" /> - <style> - .layui-col-md4 { - width: 100%; - } - - .layui-inline { - margin-left: unset; - } - - .layui-layer-dialog .layui-layer-content { - border-bottom: solid 1px #eae5e5; - } - .btns button{ - width: 120px; - } - - </style> -</head> - -<body class="pdgxq-body"> - <div class="i-container"> - <div class="jmkt-main"> - <div class="layui-fluid"> - <div class="pdgxq-m1 layui-row layui-col-space20"> - <!--pdgxq-m1-left end--> - <div class="layui-col-lg12 layui-col-md12 img-main" id="img-main" style="padding: 20px;"> - <div class="pdgxq-m1-left pdgxq-m1-box" style="width: 100%;"> - <form class="layui-form" id="form-param" lay-filter="form-param" action=""> - <div class="layui-row" style="padding: 20px 0;"> - - <div class="layui-col-md4"> - <div class="layui-inline" style="margin-left: -16%"> - <label class="layui-form-label">SNMP鍔熻兘锛�</label> - <div class="layui-input-block"> - <input type="checkbox" id="enable" name="enable" lay-skin="switch" - lay-filter="switch" lay-text="鍚敤|鍋滅敤"> - </div> - </div> - </div> - <div class="layui-col-md4"> - <div class="layui-inline"> - <label class="layui-form-label">鐗堟湰鍙凤細</label> - <div class="layui-input-block"> - <input id="ver" type="text" name="ver" value="V2.0" - class="layui-input" readonly> - </div> - </div> - </div> - <div class="layui-col-md4"> - <div class="layui-inline"> - <label class="layui-form-label">鍏辩敤浣撳悕绉帮細</label> - <div class="layui-input-block"> - <input id="publicName" type="text" name="publicName" value="public" - class="layui-input" readonly> - </div> - </div> - </div> - <div class="layui-col-md4"> - <div class="layui-inline"> - <label class="layui-form-label">绔彛鍙凤細</label> - <div class="layui-input-block"> - <input id="port" type="text" name="port" value="161" - class="layui-input" readonly> - </div> - </div> - </div> - - <div class="layui-col-md4" style=""> - <div class="layui-inline"> - <div class="layui-input-block btns" style="margin-left: 15px"> - - <button style="margin-right: 50px; font-size: 16px" - class="layui-btn layui-btn-warm" - onclick="javaScript:downLoadF(); return false;">瀵煎嚭OID琛� - </button> - </div> - </div> - </div> - - </div> - </form> - </div> - </div> - - </div> - <!--pdgxq-m1 end--> - </div> - </div> - <!--jmkt-main end--> - </div> - <!--i-container end--> - - <script type="text/javascript" src="js/jquery.min.js"></script> - <script src="plugins/layui/layui.js"></script> - <script src="js/constant.js"></script> - <script type="text/javascript"> - - var layer;// 瀹氫箟鍏ㄥ眬鍙橀噺 - var form; - var laydate; - $(function () { - layui.use(['layer', 'form', 'laydate'], function () { - layer = layui.layer; - form = layui.form; - laydate = layui.laydate; - - form.on('switch(switch)', function(data){ - //寮�鍏崇姸鎬佹敼鍙樻椂瑙﹀彂 - changeStatus(this.checked); - }); - query(); - }); - }); - - function query() { - $.ajaxSettings.async = false; - $.get("./cgi-bin/snmp/query", function (data, status) { - if ("success" == status) { - form.val('form-param', { - "enable": data.enable//0-鍚敤锛�1-绂佺敤 - }); - form.render(); - } else { - window.parent.parent.notify("绯荤粺鑾峰彇鐩戞帶璁惧淇℃伅澶辫触锛�"); - } - }, "json"); - } - - /** - * 淇敼鐘舵�� - * @param enable true or false - */ - function changeStatus(enable) { - var obj = {"enable":enable}; - $.post("./cgi-bin/snmp/enable", JSON.stringify(obj), function (data, status) { - if (data.code == "success") { - //window.parent.parent.notify("鏁版嵁淇濆瓨鎴愬姛"); - window.parent.parent.notify("SNMP妯$粍鐘舵�佷繚瀛樻垚鍔�"); - } else { - window.parent.parent.notify("鏁版嵁淇濆瓨鍑洪敊锛岃閲嶆柊鎿嶄綔锛�"+data.msg+"锛�"); - } - }, "json"); - } - - //瀵煎嚭 - function downLoadF() { - $.ajaxSettings.async = false; - $.get("./cgi-bin/snmp/download", function (data, status) { - if ("success" == data.code) { - downLoadF2(); - }else{ - window.parent.parent.notify("鐢熸垚oid琛ㄥ嚭閿�"); - } - }, "json"); - } - - function downLoadF2() { - var fileName = "oid_list.csv"; - var url = "./cgi-bin/download/download.cgi?filename=/tmp/oid_list.csv"; - - // 瀵逛簬<a>鏍囩锛屽彧鏈� Firefox 鍜� Chrome锛堝唴鏍革級 鏀寔 download 灞炴�� - if ('download' in document.createElement('a')) { // 鏀寔a鏍囩download鐨勬祻瑙堝櫒 - var link = document.createElement('a'); // 鍒涘缓a鏍囩 - link.download = fileName;// a鏍囩娣诲姞灞炴�� - link.style.display = 'none'; - // link.href = URL.createObjectURL(blob); - link.href = url; - document.body.appendChild(link); - link.click(); // 鎵ц涓嬭浇 - URL.revokeObjectURL(link.href); // 閲婃斁url - document.body.removeChild(link); // 閲婃斁鏍囩 - } else { // 鍏朵粬娴忚鍣� - navigator.msSaveBlob(url, fileName); - } - } - </script> -</body> -</html> +<!DOCTYPE html> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +<meta name="viewport" + content="width=device-width, initial-scale=1, maximum-scale=1"> +<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> +<meta name="renderer" content="webkit"> +<title>鏅鸿兘杩愮淮绠$悊绯荤粺-SNMP</title> + + <link rel="stylesheet" type="text/css" href="plugins/layui/css/layui.css" /> + <link rel="stylesheet" type="text/css" href="css/page/device-control.css" /> + <style> + .layui-col-md4 { + width: 100%; + } + + .layui-inline { + margin-left: unset; + } + + .layui-layer-dialog .layui-layer-content { + border-bottom: solid 1px #eae5e5; + } + .btns button{ + width: 120px; + } + @media only screen and (max-width:1080px) and (max-height: 1920px){ + .i-container { + min-width: 750px; + height: 100%; + overflow-y: scroll; + } + /** 闅愯棌鍒楄〃涓嬫媺婊氬姩鏉� **/ + .div-body::-webkit-scrollbar { + display: none; + } + /*IE 10+ ----闅愯棌婊氬姩鏉�*/ + .div-body { + -ms-overflow-style: none; + } + /*Firefox ----闅愯棌婊氬姩鏉�*/ + .div-body { + scrollbar-width: none; + } + .pdgxq-m1-left{ + height: 1400px; + } + } + </style> +</head> + +<body class="pdgxq-body"> + <div class="i-container div-body"> + <div class="jmkt-main"> + <div class="layui-fluid"> + <div class="pdgxq-m1 layui-row layui-col-space20"> + <!--pdgxq-m1-left end--> + <div class="layui-col-lg12 layui-col-md12 img-main" id="img-main" style="padding: 20px;"> + <div class="pdgxq-m1-left pdgxq-m1-box" style="width: 100%;"> + <form class="layui-form" id="form-param" lay-filter="form-param" action=""> + <div class="layui-row" style="padding: 20px 0;"> + + <div class="layui-col-md4"> + <div class="layui-inline" style="margin-left: -16%"> + <label class="layui-form-label">SNMP妯$粍锛�</label> + <div class="layui-input-block"> + <input type="checkbox" id="enable" name="enable" lay-skin="switch" + lay-filter="switch" lay-text="鍚敤|鍋滅敤"> + </div> + </div> + </div> + <div class="layui-col-md4"> + <div class="layui-inline"> + <label class="layui-form-label">鐗堟湰鍙凤細</label> + <div class="layui-input-block"> + <input id="ver" type="text" name="ver" value="V2.0" + class="layui-input" readonly> + </div> + </div> + </div> + <div class="layui-col-md4"> + <div class="layui-inline"> + <label class="layui-form-label">鍏辩敤浣撳悕绉帮細</label> + <div class="layui-input-block"> + <input id="publicName" type="text" name="publicName" value="public" + class="layui-input" readonly> + </div> + </div> + </div> + <div class="layui-col-md4"> + <div class="layui-inline"> + <label class="layui-form-label">绔彛鍙凤細</label> + <div class="layui-input-block"> + <input id="port" type="text" name="port" value="161" + class="layui-input" readonly> + </div> + </div> + </div> + + <div class="layui-col-md4" style=""> + <div class="layui-inline"> + <div class="layui-input-block btns" style="margin-left: 15px"> + + <button style="margin-right: 50px; font-size: 16px" + class="layui-btn layui-btn-warm" + onclick="javaScript:downLoadF(); return false;">瀵煎嚭OID琛� + </button> + </div> + </div> + </div> + + </div> + </form> + </div> + </div> + + </div> + <!--pdgxq-m1 end--> + </div> + </div> + <!--jmkt-main end--> + </div> + <!--i-container end--> + + <script type="text/javascript" src="js/jquery.min.js"></script> + <script src="plugins/layui/layui.js"></script> + <script src="js/constant.js"></script> + + <script src="plugins/external/jszip.min.js"></script> + <script src="plugins/external/FileSaver.js"></script> + <script src="plugins/external/excel-gen.js"></script> + + <script type="text/javascript"> + + var layer;// 瀹氫箟鍏ㄥ眬鍙橀噺 + var form; + var laydate; + $(function () { + layui.use(['layer', 'form', 'laydate'], function () { + layer = layui.layer; + form = layui.form; + laydate = layui.laydate; + + form.on('switch(switch)', function(data){ + //寮�鍏崇姸鎬佹敼鍙樻椂瑙﹀彂 + changeStatus(this.checked); + }); + query(); + }); + }); + + function query() { + $.ajaxSettings.async = false; + $.get("./cgi-bin/snmp/query", function (data, status) { + if ("success" == status) { + form.val('form-param', { + "enable": data.enable//0-鍚敤锛�1-绂佺敤 + }); + form.render(); + } else { + window.parent.parent.notify("绯荤粺鑾峰彇鐩戞帶璁惧淇℃伅澶辫触锛�"); + } + }, "json"); + } + + /** + * 淇敼鐘舵�� + * @param enable true or false + */ + function changeStatus(enable) { + var obj = {"enable":enable}; + $.post("./cgi-bin/snmp/enable", JSON.stringify(obj), function (data, status) { + if (data.code == "success") { + window.parent.parent.notify("鏁版嵁淇濆瓨鎴愬姛"); + } else { + window.parent.parent.notify("鏁版嵁淇濆瓨鍑洪敊锛岃閲嶆柊鎿嶄綔锛�"+data.msg+"锛�"); + } + }, "json"); + } + + //瀵煎嚭 + function downLoadF() { + $.ajaxSettings.async = false; + $.get("./cgi-bin/snmp/query_oid", function (data, status) { + renderHtml(data); + // if ("success" == data.code) { + // renderHtml(data); + // }else{ + // window.parent.parent.notify("鐢熸垚oid琛ㄥ嚭閿�"); + // } + }, "json"); + } + + function renderHtml(data) { + console.log(data); + + + var thead = ""; + thead += "<thead><tr>"; + thead += "<th>璁惧鍚嶇О</th>"; + thead += "<th>OID</th>"; + thead += "<th>璁惧ID</th>"; + thead += "<th>璁惧绫诲瀷</th>"; + thead += "<th>閫氶亾鍙�</th>"; + thead += "<th>鏁版嵁鍚嶇О</th>"; + thead += "<th>澶囨敞</th>"; + thead += "</tr></thead>"; + + var tbody = ""; + if (data != null && data.length > 0) { + $.each(data, function (index, item) { + tbody += "<tr><td>" + item.device + "</td>"; + tbody += "<td>" + item.oid + "</td>"; + tbody += "<td>" + item.index1 + "</td>"; + tbody += "<td>" + item.type + "</td>"; + tbody += "<td>" + item.index2 + "</td>"; + tbody += "<td>" + item.param + "</td>"; + tbody += "<td>" + (item.unit ?? "") + "</td></tr>"; + }); + }else { + tbody += '<tr><td colspan="8">鏆傛湭鍒涘缓璁惧</td></tr>'; + } + var html = thead + tbody; + + tableToExcel(html); + } + + + function tableToExcel(html) { + var name = "鍛婅璁板綍"; + var uri = 'data:application/vnd.ms-excel;base64,'; + var template = '<html><head><meta charset="UTF-8"></head><body><table border="1">{table}</table></body></html>'; + + if(!html){ + layer.msg("娌℃湁鑾峰彇鍒拌瀵煎嚭鐨勫唴瀹癸紒"); + } + + var ctx = {worksheet: name || 'Worksheet', table: html}; + window.location.href = uri + base64(format(template, ctx)) + } + function format(s, c) { + return s.replace(/{(\w+)}/g,function(m, p) { return c[p]; }); + } + + function base64(s) { + return window.btoa(unescape(encodeURIComponent(s))) +}; + </script> +</body> +</html> -- Gitblit v1.9.3