Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add STRING number support #1166

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions mackerel-plugin-snmp/lib/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ func (m SNMPPlugin) FetchMetrics() (map[string]interface{}, error) {

ret, err := strconv.ParseFloat(fmt.Sprint(resp.Variables[0].Value), 64)
if err != nil {
log.Println(err)
continue
// NOTE: Cannot assume strconv.ParseFloat("%s", resp.Variables[0].Value)
// first, as resp.Variables[0].Value may be an int class, etc.
// Normally, an object values such as INTEGER or Counter are
// successfully accepted in the above conversions.
// However, STRING object values are passed as byte arrays, so the above
// conversion will result in an error.
ret, err = strconv.ParseFloat(fmt.Sprintf("%s", resp.Variables[0].Value), 64)
if err != nil {
log.Println(err)
continue
}
lufia marked this conversation as resolved.
Show resolved Hide resolved
}

stat[sm.Metrics.Name] = ret
Expand Down
1 change: 1 addition & 0 deletions mackerel-plugin-snmp/rule.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
snmp.hrSystemNumUsers >=0
snmp.hrSystemProcesses >=0
snmp.echo >0
2 changes: 1 addition & 1 deletion mackerel-plugin-snmp/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ docker run --name "test-$plugin" -v $(pwd)/testdata/snmpd.conf:/etc/snmp/snmpd.c
trap 'docker stop test-$plugin; docker rm test-$plugin; exit 1' 1 2 3 15
sleep 10

$plugin '.1.3.6.1.2.1.25.1.5.0:hrSystemNumUsers:0:0' '.1.3.6.1.2.1.25.1.6.0:hrSystemProcesses:0:0' | graphite-metric-test -f rule.txt
$plugin '.1.3.6.1.2.1.25.1.5.0:hrSystemNumUsers:0:0' '.1.3.6.1.2.1.25.1.6.0:hrSystemProcesses:0:0' '.1.3.6.1.4.1.8072.1.3.2.3.1.2.4.101.99.104.111:echo:0:0' | graphite-metric-test -f rule.txt
status=$?

docker stop "test-$plugin"
Expand Down
11 changes: 7 additions & 4 deletions mackerel-plugin-snmp/testdata/snmpd.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1

rocommunity public default -V systemonly
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1
view systemonly included .1.3.6.1.4.1.8072.1.3.2.3.1.2.4

rocommunity public default -V systemonly

extend echo /bin/echo '3.14'
Loading