我们如何在 linux 中动态更新 MIB 变量值?
How we can update MIB variables values dynamically in linux?
我创建了一个 mib 文件,其中添加了五个变量。也可以通过使用以下命令
snmptranslate -m +GET-LATEST-SIGNALS-MIB -IR -On oversightInteger
我能够看到所有变量的 OID。
在此之后,通过使用“PASS 协议”,我尝试借助以下脚本文件从一个变量中获取数据
#!/bin/sh -f
PLACE=".1.3.6.1.4.1.53864.1.1"
REQ="" # Requested OID
#
# Process SET requests by simply logging the assigned value
# Note that such "assignments" are not persistent,
# nor is the syntax or requested value validated
#
if [ "" = "-s" ]; then
echo $* >> /tmp/passtest.log
exit 0
fi
#
# GETNEXT requests - determine next valid instance
#
if [ "" = "-n" ]; then
case "$REQ" in
$PLACE| \
$PLACE.0| \
$PLACE.0.*| \
$PLACE.1) RET=$PLACE.1.0 ;; # netSnmpPassString.0
$PLACE.1.*| \
$PLACE.2| \
$PLACE.2.0| \
$PLACE.2.0.*| \
$PLACE.2.1| \
$PLACE.2.1.0| \
$PLACE.2.1.0.*| \
$PLACE.2.1.1| \
$PLACE.2.1.1.*| \
$PLACE.2.1.2| \
$PLACE.2.1.2.0) RET=$PLACE.2.1.2.1 ;; # netSnmpPassInteger.1
$PLACE.2.1.2.*| \
$PLACE.2.1.3| \
$PLACE.2.1.3.0) RET=$PLACE.2.1.3.1 ;; # netSnmpPassOID.1
$PLACE.2.*| \
$PLACE.3) RET=$PLACE.3.0 ;; # netSnmpPassTimeTicks.0
$PLACE.3.*| \
$PLACE.4) RET=$PLACE.4.0 ;; # netSnmpPassIpAddress.0
$PLACE.4.*| \
$PLACE.5) RET=$PLACE.5.0 ;; # netSnmpPassCounter.0
$PLACE.5.*| \
$PLACE.6) RET=$PLACE.6.0 ;; # netSnmpPassGauge.0
*) exit 0 ;;
esac
else
#
# GET requests - check for valid instance
#
case "$REQ" in
$PLACE.1.0| \
$PLACE.2.1.2.1| \
$PLACE.2.1.3.1| \
$PLACE.3.0| \
$PLACE.4.0| \
$PLACE.5.0| \
$PLACE.6.0) RET=$REQ ;;
*) exit 0 ;;
esac
fi
#
# "Process" GET* requests - return hard-coded value
#
echo "$RET"
case "$RET" in
$PLACE.1.0) echo "string"; echo "Life, the Universe, and Everything"; exit 0 ;;
$PLACE.2.1.2.1) echo "integer"; echo "42"; exit 0 ;;
$PLACE.2.1.3.1) echo "objectid"; echo "$PLACE.99"; exit 0 ;;
$PLACE.3.0) echo "timeticks"; echo "363136200"; exit 0 ;;
$PLACE.4.0) echo "ipaddress"; echo "127.0.0.1"; exit 0 ;;
$PLACE.5.0) echo "counter"; echo "42"; exit 0 ;;
$PLACE.6.0) echo "gauge"; echo "42"; exit 0 ;;
*) echo "string"; echo "ack... $RET $REQ"; exit 0 ;; # Should not happen
esac
在运行上面的脚本文件之后我使用了下面的命令
snmpwalk -v2c localhost -c public .1.3.6.1.4.1.53864
我得到的输出如下
GET-LATEST-SIGNALS-MIB::oversightInteger.1.0 = STRING: "Life, the
Universe, and Everything"
GET-LATEST-SIGNALS-MIB::oversightInteger.2.1.2.1 = Wrong Type
(should be OCTET STRING): INTEGER: 42
GET-LATEST-SIGNALS-MIB::oversightInteger.2.1.3.1 = Wrong Type
(should be OCTET STRING): OID: GET-LATEST-SIGNALS-
MIB::oversightInteger.99
GET-LATEST-SIGNALS-MIB::oversightInteger.3.0 = Wrong Type (should
be OCTET STRING): Timeticks: (363136200) 42 days, 0:42:42.00
GET-LATEST-SIGNALS-MIB::oversightInteger.4.0 = Wrong Type (should
be OCTET STRING): IpAddress: 127.0.0.1
GET-LATEST-SIGNALS-MIB::oversightInteger.5.0 = Wrong Type (should
e OCTET STRING): Counter32: 42
GET-LATEST-SIGNALS-MIB::oversightInteger.6.0 = Wrong Type (should
be OCTET STRING): Gauge32: 42
所以现在我想动态更改值“Life, the Universe, and Everything” 那么我们如何动态更改 oid 值呢?
我在这一点上被困了将近 2 周,所以非常感谢您的帮助!
所以为了解决这个问题,我添加了一个 sqlite3 database
。
现在,每当我们在 embedded c application
中获取最新值(总是在 运行 后面)时,我们都会在数据库中更新相同的值,因此在 SNMPD daemon
之间收到任何请求,例如设置、获取等,然后脚本将去读取数据库以获取 mib 变量的最新值,并将发送到 mib 浏览器。
我们在脚本中使用以下命令从数据库中获取数据
echo "$(sqlite3 /home/root/database/pdu.db <<'END_SQL'
.timeout 2000
SELECT Variable_Value FROM Data Where Sr_No='2';
END_SQL)
我创建了一个 mib 文件,其中添加了五个变量。也可以通过使用以下命令
snmptranslate -m +GET-LATEST-SIGNALS-MIB -IR -On oversightInteger
我能够看到所有变量的 OID。
在此之后,通过使用“PASS 协议”,我尝试借助以下脚本文件从一个变量中获取数据
#!/bin/sh -f
PLACE=".1.3.6.1.4.1.53864.1.1"
REQ="" # Requested OID
#
# Process SET requests by simply logging the assigned value
# Note that such "assignments" are not persistent,
# nor is the syntax or requested value validated
#
if [ "" = "-s" ]; then
echo $* >> /tmp/passtest.log
exit 0
fi
#
# GETNEXT requests - determine next valid instance
#
if [ "" = "-n" ]; then
case "$REQ" in
$PLACE| \
$PLACE.0| \
$PLACE.0.*| \
$PLACE.1) RET=$PLACE.1.0 ;; # netSnmpPassString.0
$PLACE.1.*| \
$PLACE.2| \
$PLACE.2.0| \
$PLACE.2.0.*| \
$PLACE.2.1| \
$PLACE.2.1.0| \
$PLACE.2.1.0.*| \
$PLACE.2.1.1| \
$PLACE.2.1.1.*| \
$PLACE.2.1.2| \
$PLACE.2.1.2.0) RET=$PLACE.2.1.2.1 ;; # netSnmpPassInteger.1
$PLACE.2.1.2.*| \
$PLACE.2.1.3| \
$PLACE.2.1.3.0) RET=$PLACE.2.1.3.1 ;; # netSnmpPassOID.1
$PLACE.2.*| \
$PLACE.3) RET=$PLACE.3.0 ;; # netSnmpPassTimeTicks.0
$PLACE.3.*| \
$PLACE.4) RET=$PLACE.4.0 ;; # netSnmpPassIpAddress.0
$PLACE.4.*| \
$PLACE.5) RET=$PLACE.5.0 ;; # netSnmpPassCounter.0
$PLACE.5.*| \
$PLACE.6) RET=$PLACE.6.0 ;; # netSnmpPassGauge.0
*) exit 0 ;;
esac
else
#
# GET requests - check for valid instance
#
case "$REQ" in
$PLACE.1.0| \
$PLACE.2.1.2.1| \
$PLACE.2.1.3.1| \
$PLACE.3.0| \
$PLACE.4.0| \
$PLACE.5.0| \
$PLACE.6.0) RET=$REQ ;;
*) exit 0 ;;
esac
fi
#
# "Process" GET* requests - return hard-coded value
#
echo "$RET"
case "$RET" in
$PLACE.1.0) echo "string"; echo "Life, the Universe, and Everything"; exit 0 ;;
$PLACE.2.1.2.1) echo "integer"; echo "42"; exit 0 ;;
$PLACE.2.1.3.1) echo "objectid"; echo "$PLACE.99"; exit 0 ;;
$PLACE.3.0) echo "timeticks"; echo "363136200"; exit 0 ;;
$PLACE.4.0) echo "ipaddress"; echo "127.0.0.1"; exit 0 ;;
$PLACE.5.0) echo "counter"; echo "42"; exit 0 ;;
$PLACE.6.0) echo "gauge"; echo "42"; exit 0 ;;
*) echo "string"; echo "ack... $RET $REQ"; exit 0 ;; # Should not happen
esac
在运行上面的脚本文件之后我使用了下面的命令
snmpwalk -v2c localhost -c public .1.3.6.1.4.1.53864
我得到的输出如下
GET-LATEST-SIGNALS-MIB::oversightInteger.1.0 = STRING: "Life, the
Universe, and Everything"
GET-LATEST-SIGNALS-MIB::oversightInteger.2.1.2.1 = Wrong Type
(should be OCTET STRING): INTEGER: 42
GET-LATEST-SIGNALS-MIB::oversightInteger.2.1.3.1 = Wrong Type
(should be OCTET STRING): OID: GET-LATEST-SIGNALS-
MIB::oversightInteger.99
GET-LATEST-SIGNALS-MIB::oversightInteger.3.0 = Wrong Type (should
be OCTET STRING): Timeticks: (363136200) 42 days, 0:42:42.00
GET-LATEST-SIGNALS-MIB::oversightInteger.4.0 = Wrong Type (should
be OCTET STRING): IpAddress: 127.0.0.1
GET-LATEST-SIGNALS-MIB::oversightInteger.5.0 = Wrong Type (should
e OCTET STRING): Counter32: 42
GET-LATEST-SIGNALS-MIB::oversightInteger.6.0 = Wrong Type (should
be OCTET STRING): Gauge32: 42
所以现在我想动态更改值“Life, the Universe, and Everything” 那么我们如何动态更改 oid 值呢?
我在这一点上被困了将近 2 周,所以非常感谢您的帮助!
所以为了解决这个问题,我添加了一个 sqlite3 database
。
现在,每当我们在 embedded c application
中获取最新值(总是在 运行 后面)时,我们都会在数据库中更新相同的值,因此在 SNMPD daemon
之间收到任何请求,例如设置、获取等,然后脚本将去读取数据库以获取 mib 变量的最新值,并将发送到 mib 浏览器。
我们在脚本中使用以下命令从数据库中获取数据
echo "$(sqlite3 /home/root/database/pdu.db <<'END_SQL'
.timeout 2000
SELECT Variable_Value FROM Data Where Sr_No='2';
END_SQL)