Skip to content

Commit

Permalink
Query the same OIDs to various hosts
Browse files Browse the repository at this point in the history
Change `host` config key to `hosts` and accept an
array. Beater will iterate that array to query each
host.
  • Loading branch information
Isaac committed Mar 6, 2018
1 parent 515543b commit a33c71a
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 54 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ otilio:
# Defines how often an event is sent to the output
period: 1s
# SNMP host to query
host: "127.0.0.1"
# SNMP hosts to query
hosts: ["192.168.1.1", "192.168.1.2"]
# SMNP version: 1, 2c or 3
version: 2c
Expand All @@ -26,7 +26,7 @@ otilio:
- {oid: ".1.3.6.1.2.1.1.1.0", name: sysDescr}
- {oid: ".1.3.6.1.2.1.1.3.0", name: sysUpTime}
```
This will get oids `1.3.6.1.2.1.1.1.0` and `1.3.6.1.2.1.1.3.0` from SNMP server at localhost and store them in `otilio-YYYY.MM.DD` index in Elasticsearch in fields `sysDescr` and `sysUpTime`.
This will get oids `1.3.6.1.2.1.1.1.0` and `1.3.6.1.2.1.1.3.0` from SNMP servers at 192.168.1.1 and 192.168.1.2 and store them in `otilio-YYYY.MM.DD` index in Elasticsearch in fields `sysDescr` and `sysUpTime`.

SNMP V3 configuration example

Expand All @@ -36,7 +36,7 @@ otilio:
period: 1s
# SNMP host to query
host: "127.0.0.1"
hosts: ["127.0.0.1"]
port: 10161
# SMNP version
Expand Down
2 changes: 1 addition & 1 deletion _meta/beat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ otilio:
period: 1s

# SNMP host to query
host: "127.0.0.1"
hosts: ["127.0.0.1"]
port: 161

# SMNP version: 1, 2c or 3
Expand Down
83 changes: 43 additions & 40 deletions beater/otilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,51 +72,54 @@ func (bt *Otilio) Run(b *beat.Beat) error {
return nil
case <-ticker.C:
// TODO: connect outside the loop with a timeout < bt.config.Period
gosnmp.Default.Target = bt.config.Host
gosnmp.Default.Port = bt.config.Port
gosnmp.Default.Community = bt.config.Community
gosnmp.Default.Version = bt.version
if bt.version == gosnmp.Version3 {
gosnmp.Default.SecurityModel = gosnmp.UserSecurityModel
gosnmp.Default.SecurityParameters = &gosnmp.UsmSecurityParameters{
UserName: bt.config.User,
AuthenticationPassphrase: bt.config.AuthPassword,
PrivacyPassphrase: bt.config.PrivPassword,
AuthenticationProtocol: gosnmp.SHA,
PrivacyProtocol: gosnmp.DES,
for _, host := range bt.config.Hosts {
gosnmp.Default.Target = host
gosnmp.Default.Port = bt.config.Port
gosnmp.Default.Community = bt.config.Community
gosnmp.Default.Version = bt.version
if bt.version == gosnmp.Version3 {
gosnmp.Default.SecurityModel = gosnmp.UserSecurityModel
gosnmp.Default.SecurityParameters = &gosnmp.UsmSecurityParameters{
UserName: bt.config.User,
AuthenticationPassphrase: bt.config.AuthPassword,
PrivacyPassphrase: bt.config.PrivPassword,
AuthenticationProtocol: gosnmp.SHA,
PrivacyProtocol: gosnmp.DES,
}
}
}
err := gosnmp.Default.Connect()
if err != nil {
logp.Critical("Can't connect to %s: %v", bt.config.Host, err.Error())
return fmt.Errorf("Can't connect to %s", bt.config.Host)
}
defer gosnmp.Default.Conn.Close()
r, err := gosnmp.Default.Get(bt.oids)
if err != nil {
logp.Err("Can't get oids %v: %v", bt.config.OIDs, err.Error())
} else {
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": b.Name,
err := gosnmp.Default.Connect()
if err != nil {
logp.Critical("Can't connect to %s: %v", host, err.Error())
return fmt.Errorf("Can't connect to %s", host)
}
for _, v := range r.Variables {
var value interface{}
k := bt.oidToName[v.Name]
if k == "" {
k = v.Name
defer gosnmp.Default.Conn.Close()
r, err := gosnmp.Default.Get(bt.oids)
if err != nil {
logp.Err("Can't get oids for %v: %v", host, err.Error())
} else {
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": b.Name,
"snmp.host": host,
}
switch v.Type {
case gosnmp.OctetString:
value = string(v.Value.([]byte))
default:
value = gosnmp.ToBigInt(v.Value)
for _, v := range r.Variables {
var value interface{}
k := bt.oidToName[v.Name]
if k == "" {
k = v.Name
}
switch v.Type {
case gosnmp.OctetString:
value = string(v.Value.([]byte))
default:
value = gosnmp.ToBigInt(v.Value)
}
logp.Debug("otilio", "%s = %s", k, value)
event.Put(k, value)
}
logp.Debug("otilio", "%s = %s", k, value)
event.Put(k, value)
bt.client.PublishEvent(event)
logp.Info("Event sent")
}
bt.client.PublishEvent(event)
logp.Info("Event sent")
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "time"
// Config stores Otilio configuration loaded from .yaml file
type Config struct {
Period time.Duration `config:"period"`
Host string `config:"host"`
Hosts []string `config:"hosts"`
Port uint16 `config:"port"`
Community string `config:"community"`
User string `config:"user"`
Expand All @@ -21,7 +21,6 @@ type Config struct {
// DefaultConfig default configuration
var DefaultConfig = Config{
Period: 1 * time.Second,
Host: "127.0.0.1",
Port: 161,
Community: "public",
Version: "2c",
Expand Down
2 changes: 1 addition & 1 deletion otilio.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ otilio:
period: 1s

# SNMP host to query
host: "127.0.0.1"
hosts: ["127.0.0.1"]
port: 161

# SMNP version: 1, 2c or 3
Expand Down
2 changes: 1 addition & 1 deletion otilio.template-es2x.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
},
"_meta": {
"version": "5.6.8"
"version": "5.6.8-3"
},
"date_detection": false,
"dynamic_templates": [
Expand Down
2 changes: 1 addition & 1 deletion otilio.template-es6x.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"_default_": {
"numeric_detection": true,
"_meta": {
"version": "5.6.8"
"version": "5.6.8-3"
},
"date_detection": false,
"dynamic_templates": [
Expand Down
2 changes: 1 addition & 1 deletion otilio.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"_default_": {
"numeric_detection": true,
"_meta": {
"version": "5.6.8"
"version": "5.6.8-3"
},
"date_detection": false,
"dynamic_templates": [
Expand Down
2 changes: 1 addition & 1 deletion otilio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ otilio:
period: 1s

# SNMP host to query
host: "127.0.0.1"
hosts: ["127.0.0.1"]
port: 161

# SMNP version: 1, 2c or 3
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/elastic/beats/libbeat/beat/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a33c71a

Please sign in to comment.