Skip to content
Open
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
144 changes: 144 additions & 0 deletions Godeps/_workspace/src/github.com/marc-barry/goprocinfo/linux/snmp.go

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

21 changes: 21 additions & 0 deletions linuxproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
CPUInfoPath = "/proc/cpuinfo"
NetworkStatPath = "/proc/net/dev"
NetstatStatPath = "/proc/net/netstat"
SnmpStatPath = "/proc/net/snmp"
SockstatStatPath = "/proc/net/sockstat"
)

Expand Down Expand Up @@ -44,6 +45,26 @@ func getNetworkDeviceStatsList() []Stat {
return list
}

func readSnmpStats() (*linuxproc.Snmp, error) {
return linuxproc.ReadSnmp(SnmpStatPath)
}

func getSnmpStatsList() []Stat {
stat := linuxproc.Snmp{}

elem := reflect.ValueOf(&stat).Elem()
typeOfElem := elem.Type()

list := make([]Stat, 0)

for i := 0; i < elem.NumField(); i++ {
field := typeOfElem.Field(i)
list = append(list, Stat{field.Name, strings.Join([]string{snmpStatsMetricPrefix, field.Tag.Get("json")}, "")})
}

return list
}

func readNetstatStats() (*linuxproc.Netstat, error) {
return linuxproc.ReadNetstat(NetstatStatPath)
}
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func main() {
fmt.Println(stat.StatName + ":" + stat.MetricName)
}
fmt.Println("<---")
fmt.Println(SnmpStatPath)
for _, stat := range getSnmpStatsList() {
fmt.Println(stat.StatName + ":" + stat.MetricName)
}
fmt.Println("<---")
os.Exit(0)
}

Expand Down Expand Up @@ -191,6 +196,7 @@ func startLoggers() {
logNetworkDeviceStats()
logNetstatStats()
logSockstatStats()
logSnmpStats()
}
}
})
Expand All @@ -206,6 +212,7 @@ func startCollectors() {
collectNetworkDeviceStats()
collectNetstatStats()
collectSockstatStats()
collectSnmpStats()
}
}
})
Expand Down
3 changes: 3 additions & 0 deletions repodb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
classification: alpha
oncall:
- neteng
6 changes: 6 additions & 0 deletions script/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -e
set -x

script/build
42 changes: 42 additions & 0 deletions statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
networkDeviceStatsMetricPrefix = "net.dev."
netstatStatsMetricPrefix = "net.netstat."
sockstatStatsMetricPrefix = "net.sockstat."
snmpStatsMetricPrefix = "net.snmp."
)

type LoggedStat struct {
Expand Down Expand Up @@ -112,6 +113,47 @@ func logNetstatStats() {
fmt.Println(string(jsonBytes))
}

func collectSnmpStats() {
stats, err := readSnmpStats()

if err != nil {
Log.WithField("error", err).Error("Error reading SNMP stats. Can't collect snmp stats.")
}
elem := reflect.ValueOf(stats).Elem()
typeOfElem := elem.Type()

for i := 0; i < elem.NumField(); i++ {
field := typeOfElem.Field(i)

value := elem.Field(i).Uint()

_, collect := StatsMap[field.Name]

if metricName := field.Tag.Get("json"); collect && metricName != "" {
if err := StatsdClient.Gauge(strings.Join([]string{snmpStatsMetricPrefix, metricName}, ""), float64(value), []string{}, 1); err != nil {
Log.WithField("error", err).Error("Couldn't submit event to statsd.")
}
}
}
}

func logSnmpStats() {
stats, err := readSnmpStats()

if err != nil {
Log.WithField("error", err).Error("Error reading SNMP stats. Can't log snmp stats.")
}

loggedStat := LoggedStat{SnmpStatPath, time.Now(), stats}

jsonBytes, err := json.Marshal(loggedStat)
if err != nil {
Log.WithField("error", err).Errorf("Error JSON marshalling: %+v.", stats)
}

fmt.Println(string(jsonBytes))
}

func collectSockstatStats() {
stats, err := readSockstatStats()

Expand Down