@@ -3,6 +3,7 @@ package sensu
3
3
import (
4
4
"fmt"
5
5
"log"
6
+ "os"
6
7
"plugins"
7
8
"plugins/checks"
8
9
"plugins/metrics"
@@ -18,6 +19,7 @@ type PluginProcessor struct {
18
19
jobsConfig map [string ]plugins.PluginConfig
19
20
close chan bool
20
21
results chan * Result
22
+ logger * log.Logger
21
23
}
22
24
23
25
// used to create a new processor instance.
@@ -26,6 +28,7 @@ func NewPluginProcessor() *PluginProcessor {
26
28
proc .jobs = make (map [string ]plugins.SensuPluginInterface )
27
29
proc .jobsConfig = make (map [string ]plugins.PluginConfig )
28
30
proc .results = make (chan * Result , 500 ) // queue of 500 buffered results
31
+ proc .logger = log .New (os .Stdout , "Plugin: " , log .LstdFlags )
29
32
30
33
return proc
31
34
}
@@ -78,10 +81,10 @@ func newCheckConfig(json interface{}) plugins.PluginConfig {
78
81
func (p * PluginProcessor ) AddJob (job plugins.SensuPluginInterface , checkConfig plugins.PluginConfig ) {
79
82
name , err := job .Init (checkConfig )
80
83
if nil != err {
81
- log .Printf ("Failed to initialise check: (%s) %s\n " , name , err )
84
+ p . logger .Printf ("Failed to initialise check: (%s) %s\n " , name , err )
82
85
return
83
86
}
84
- log .Printf ("Scheduling job: %s (%s) every %d seconds" , name , checkConfig .Command , checkConfig .Interval )
87
+ p . logger .Printf ("Scheduling job: %s (%s) every %d seconds" , name , checkConfig .Command , checkConfig .Interval )
85
88
86
89
p .jobs [name ] = job
87
90
p .jobsConfig [name ] = checkConfig
@@ -107,39 +110,12 @@ func (p *PluginProcessor) Init(q MessageQueuer, config *Config) error {
107
110
for check_type , checkConfigInterface := range checks_config {
108
111
checkConfig , ok := checkConfigInterface .(map [string ]interface {})
109
112
if ! ok {
110
- log .Printf ("Failed to parse config: %" , check_type )
113
+ p . logger .Printf ("Failed to parse config: %" , check_type )
111
114
continue
112
115
}
113
116
114
117
config := newCheckConfig (checkConfig )
115
-
116
- // see if we can handle this check using one of our build in ones
117
- switch check_type {
118
- case "cpu_metrics" :
119
- check = new (metrics.CpuStats )
120
- case "display_metrics" :
121
- check = new (metrics.DisplayStats )
122
- case "interface_metrics" :
123
- check = new (metrics.NetworkInterfaceStats )
124
- case "load_metrics" :
125
- check = new (metrics.LoadStats )
126
- case "memory_metrics" :
127
- check = new (metrics.MemoryStats )
128
- case "uptime_metrics" :
129
- check = new (metrics.UptimeStats )
130
- case "wireless-ap_metrics" :
131
- check = new (metrics.WirelessStats )
132
- case "check_procs" :
133
- check = new (checks.ProcessCheck )
134
- default :
135
- if "metric" == config .Type {
136
- // we have a metric!
137
- check = new (metrics.ExternalMetric )
138
- } else {
139
- // we have a check!
140
- check = new (checks.ExternalCheck )
141
- }
142
- }
118
+ check = getCheckHandler (check_type , config .Type )
143
119
144
120
config .Name = check_type
145
121
p .AddJob (check , config )
@@ -153,17 +129,18 @@ func (p *PluginProcessor) Init(q MessageQueuer, config *Config) error {
153
129
func (p * PluginProcessor ) Start () {
154
130
go p .publish ()
155
131
132
+ clientConfig := p .config .Data ().Get ("client" )
133
+
156
134
// start our result publisher thread
157
135
for job_name , job := range p .jobs {
158
136
go func (theJobName string , theJob plugins.SensuPluginInterface ) {
159
137
config := p .jobsConfig [job_name ]
160
138
161
- log .Printf ("Starting job: %s" , theJobName )
162
139
reset := make (chan bool )
163
140
164
141
timer := time .AfterFunc (0 , func () {
165
- log .Printf ("Gathering: %s" , theJobName )
166
- result := NewResult (p . config . Data (). Get ( "client" ) , theJobName )
142
+ p . logger .Printf ("Gathering: %s" , theJobName )
143
+ result := NewResult (clientConfig , theJobName )
167
144
result .SetCommand (config .Command )
168
145
169
146
presult := new (plugins.Result )
@@ -174,7 +151,7 @@ func (p *PluginProcessor) Start() {
174
151
175
152
if nil != err {
176
153
// returned an error - we should stop this job from running
177
- log .Printf ("Failed to gather stat: %s. %v" , theJobName , err )
154
+ p . logger .Printf ("Failed to gather stat: %s. %v" , theJobName , err )
178
155
reset <- false
179
156
return
180
157
}
@@ -216,11 +193,42 @@ func (p *PluginProcessor) publish() {
216
193
case result := <- p .results :
217
194
if result .HasOutput () {
218
195
if err := p .q .Publish (RESULTS_QUEUE , "" , result .GetPayload ()); err != nil {
219
- log .Printf ("Error Publishing Stats: %v. %v" , err , result )
196
+ p . logger .Printf ("Error Publishing Stats: %v. %v" , err , result )
220
197
}
221
198
}
222
199
case <- p .close :
223
200
return
224
201
}
225
202
}
226
203
}
204
+
205
+ func getCheckHandler (check_type , config_type string ) plugins.SensuPluginInterface {
206
+ var check plugins.SensuPluginInterface
207
+ switch check_type {
208
+ case "cpu_metrics" , "cpu_metrics.rb" :
209
+ check = new (metrics.CpuStats )
210
+ case "display_metrics" :
211
+ check = new (metrics.DisplayStats )
212
+ case "interface_metrics" :
213
+ check = new (metrics.NetworkInterfaceStats )
214
+ case "load_metrics" :
215
+ check = new (metrics.LoadStats )
216
+ case "memory_metrics" :
217
+ check = new (metrics.MemoryStats )
218
+ case "uptime_metrics" :
219
+ check = new (metrics.UptimeStats )
220
+ case "wireless-ap_metrics" :
221
+ check = new (metrics.WirelessStats )
222
+ case "check_procs" :
223
+ check = new (checks.ProcessCheck )
224
+ default :
225
+ if "metric" == config_type {
226
+ // we have a metric!
227
+ check = new (metrics.ExternalMetric )
228
+ } else {
229
+ // we have a check!
230
+ check = new (checks.ExternalCheck )
231
+ }
232
+ }
233
+ return check
234
+ }
0 commit comments