-
Notifications
You must be signed in to change notification settings - Fork 13
/
goshin.go
186 lines (152 loc) · 3.99 KB
/
goshin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package goshin
import (
"fmt"
"github.com/amir/raidman"
"github.com/tjgq/broadcast"
"log/syslog"
"math"
"strings"
"time"
)
var logger, _ = syslog.New(syslog.LOG_DAEMON, "goshin")
type Metric struct {
Service, Description, State string
Value interface{}
}
func NewMetric() *Metric {
return &Metric{State: "ok"}
}
type Threshold struct {
Warning, Critical float64
}
func NewThreshold() *Threshold {
return &Threshold{}
}
type Goshin struct {
Address string
EventHost string
Interval int
Tag []string
Ttl float32
Ifaces map[string]bool
IgnoreIfaces map[string]bool
Devices map[string]bool
IgnoreDevices map[string]bool
Thresholds map[string]*Threshold
Checks map[string]bool
ConnectionType string
Timeout int
}
func NewGoshin() *Goshin {
return &Goshin{
Thresholds: make(map[string]*Threshold),
}
}
func (g *Goshin) Start() {
defer logger.Close()
cputime := NewCPUTime()
memoryusage := NewMemoryUsage()
loadaverage := NewLoadAverage()
netstats := NewNetStats(g.Ifaces, g.IgnoreIfaces)
diskspace := NewDiskSpace()
diskstats := NewDiskStats(g.Devices, g.IgnoreDevices)
logger.Info(fmt.Sprintf("starting Goshin : will report each %d seconds", g.Interval))
// channel size has to be large enough
// to allow Goshin send all metrics to Riemann
// in g.Interval
var collectQueue chan *Metric = make(chan *Metric, 100)
ticker := time.NewTicker(time.Second * time.Duration(g.Interval))
b := broadcast.New(10)
if g.Checks["cpu"] {
logger.Debug("collector 'cpu' is enabled")
go cputime.Collect(collectQueue, b.Listen())
}
if g.Checks["memory"] {
logger.Debug("collector 'memory' is enabled")
go memoryusage.Collect(collectQueue, b.Listen())
}
if g.Checks["load"] {
logger.Debug("collector 'load' is enabled")
go loadaverage.Collect(collectQueue, b.Listen())
}
if g.Checks["net"] {
logger.Debug("collector 'net' is enabled")
go netstats.Collect(collectQueue, b.Listen())
}
if g.Checks["disk"] {
logger.Debug("collector 'disk' is enabled")
go diskspace.Collect(collectQueue, b.Listen())
}
if g.Checks["diskstats"] {
logger.Debug("collector 'diskstats' is enabled")
go diskstats.Collect(collectQueue, b.Listen())
}
go g.Report(collectQueue)
for t := range ticker.C {
b.Send(t)
}
}
func (g *Goshin) EnforceState(metric *Metric) {
// disk /boot => disk
// cpu => cpu
service := strings.Split(metric.Service, " ")[0]
value := metric.Value
typeofvalue := fmt.Sprintf("%T", value)
if strings.HasPrefix(typeofvalue, "float") {
metric.Value = RoundPlus(value.(float64), 2)
}
threshold, present := g.Thresholds[service]
if present {
// only for int and float type
switch {
case value.(float64) > threshold.Critical:
metric.State = "critical"
case value.(float64) > threshold.Warning:
metric.State = "warning"
default:
metric.State = "ok"
}
}
}
func (g *Goshin) Report(reportQueue chan *Metric) {
connected := false
var connError error
var c *raidman.Client
for {
if connected == false {
c, connError = raidman.DialWithTimeout(g.ConnectionType, g.Address, time.Duration(g.Timeout) * time.Second)
}
if connError != nil {
logger.Err(fmt.Sprintf("error : can not connect to host %s", g.Address))
connected = false
} else {
connected = true
}
metric := <-reportQueue
if connected {
g.EnforceState(metric)
connError = c.Send(&raidman.Event{
Metric: metric.Value,
Ttl: g.Ttl,
Service: metric.Service,
Description: metric.Description,
Tags: g.Tag,
Host: g.EventHost,
State: metric.State})
if connError != nil {
logger.Err(fmt.Sprintf("error : %s", connError))
c.Close()
connected = false
}
}
metric = nil
}
}
// https://gist.github.com/DavidVaini/10308388#gistcomment-1391788
func Round(f float64) float64 {
return math.Floor(f + .5)
}
func RoundPlus(f float64, places int) float64 {
shift := math.Pow(10, float64(places))
return Round(f*shift) / shift
}