forked from adejoux/nmon2influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmon.go
253 lines (216 loc) · 5.86 KB
/
nmon.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// nmon2influxdb
// import nmon data in InfluxDB
// author: adejoux@djouxtech.net
package main
import (
"bufio"
"errors"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// Nmon structure used to manage nmon files
type Nmon struct {
Hostname string
OS string
TimeStamps map[string]string
TextContent string
DataSeries map[string]DataSerie
Debug bool
Config *Config
starttime time.Time
stoptime time.Time
Location *time.Location
}
// DataSerie structure contains the columns and points to insert in InfluxDB
type DataSerie struct {
Columns []string
}
// AppendText add text section to dashboard
func (nmon *Nmon) AppendText(text string) {
nmon.TextContent += ReplaceComma(text)
}
// NewNmon initialize a Nmon structure
func NewNmon() *Nmon {
return &Nmon{DataSeries: make(map[string]DataSerie), TimeStamps: make(map[string]string)}
}
// BuildPoint create a point and convert string value to float when possible
func (nmon *Nmon) BuildPoint(serie string, values []string) map[string]interface{} {
columns := nmon.DataSeries[serie].Columns
//TODO check output
point := make(map[string]interface{})
for i, rawvalue := range values {
// try to convert string to integer
value, err := strconv.ParseFloat(rawvalue, 64)
if err != nil {
//if not working, use string
point[columns[i]] = rawvalue
} else {
//send integer if it worked
point[columns[i]] = value
}
}
return point
}
//GetTimeStamp retrieves the TimeStamp corresponding to the entry
func (nmon *Nmon) GetTimeStamp(label string) (timeStamp string, err error) {
if t, ok := nmon.TimeStamps[label]; ok {
timeStamp = t
} else {
errorMessage := fmt.Sprintf("TimeStamp %s not found", label)
err = errors.New(errorMessage)
}
return
}
//InitNmonTemplate init nmon structure when creating dashboard
func InitNmonTemplate(config *Config) (nmon *Nmon) {
nmon = NewNmon()
nmon.Config = config
nmon.SetLocation(config.Timezone)
return
}
//InitNmon init nmon structure for nmon file import
func InitNmon(config *Config, nmonFile NmonFile) (nmon *Nmon) {
nmon = NewNmon()
nmon.Config = config
nmon.SetLocation(config.Timezone)
nmon.Debug = config.Debug
var lines []string
if len(nmonFile.Host) > 0 {
scanner, err := nmonFile.GetRemoteScanner()
check(err)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
scanner.Close()
} else {
scanner, err := nmonFile.GetScanner()
check(err)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
scanner.Close()
}
var userSkipRegexp *regexp.Regexp
if len(config.ImportSkipMetrics) > 0 {
skipped := strings.Replace(config.ImportSkipMetrics, ",", "|", -1)
userSkipRegexp = regexp.MustCompile(skipped)
}
for _, line := range lines {
if cpuallRegexp.MatchString(line) && !config.ImportAllCpus {
continue
}
if diskallRegexp.MatchString(line) && config.ImportSkipDisks {
continue
}
if timeRegexp.MatchString(line) {
matched := timeRegexp.FindStringSubmatch(line)
nmon.TimeStamps[matched[1]] = matched[2]
continue
}
if hostRegexp.MatchString(line) {
matched := hostRegexp.FindStringSubmatch(line)
nmon.Hostname = strings.ToLower(matched[1])
continue
}
if osRegexp.MatchString(line) {
matched := osRegexp.FindStringSubmatch(line)
nmon.OS = strings.ToLower(matched[1])
continue
}
if infoRegexp.MatchString(line) {
matched := infoRegexp.FindStringSubmatch(line)
nmon.AppendText(matched[1])
continue
}
if !headerRegexp.MatchString(line) {
if len(line) == 0 {
continue
}
if badRegexp.MatchString(line) {
continue
}
elems := strings.Split(line, ",")
if len(elems) < 3 {
if config.Debug == true {
fmt.Printf("ERROR: parsing the following line : %s\n", line)
}
continue
}
name := elems[0]
if len(config.ImportSkipMetrics) > 0 {
if userSkipRegexp.MatchString(name) {
continue
}
}
if config.Debug == true {
fmt.Printf("ADDING serie %s\n", name)
}
dataserie := nmon.DataSeries[name]
dataserie.Columns = elems[2:]
nmon.DataSeries[name] = dataserie
}
}
return
}
//SetTimeFrame set the current timeframe for the dashboard
func (nmon *Nmon) SetTimeFrame() {
keys := make([]string, 0, len(nmon.TimeStamps))
for k := range nmon.TimeStamps {
keys = append(keys, k)
}
sort.Strings(keys)
nmon.starttime, _ = nmon.ConvertTimeStamp(nmon.TimeStamps[keys[0]])
nmon.stoptime, _ = nmon.ConvertTimeStamp(nmon.TimeStamps[keys[len(keys)-1]])
}
// StartTime returns the starting timestamp for dashboard
func (nmon *Nmon) StartTime() string {
if nmon.starttime == (time.Time{}) {
nmon.SetTimeFrame()
}
return nmon.starttime.UTC().Format(time.RFC3339)
}
// StopTime returns the ending timestamp for dashboard
func (nmon *Nmon) StopTime() string {
if nmon.stoptime == (time.Time{}) {
nmon.SetTimeFrame()
}
return nmon.stoptime.UTC().Format(time.RFC3339)
}
const timeformat = "15:04:05,02-Jan-2006"
//SetLocation set the timezone used to input metrics in InfluxDB
func (nmon *Nmon) SetLocation(tz string) (err error) {
var loc *time.Location
if len(tz) > 0 {
loc, err = time.LoadLocation(tz)
if err != nil {
loc = time.FixedZone("Europe/Paris", 2*60*60)
}
} else {
timezone, _ := time.Now().In(time.Local).Zone()
loc, err = time.LoadLocation(timezone)
if err != nil {
loc = time.FixedZone("Europe/Paris", 2*60*60)
}
}
nmon.Location = loc
return
}
//ConvertTimeStamp convert the string timestamp in time.Time structure
func (nmon *Nmon) ConvertTimeStamp(s string) (time.Time, error) {
var err error
if s == "now" {
return time.Now().Truncate(24 * time.Hour), err
}
t, err := time.ParseInLocation(timeformat, s, nmon.Location)
return t, err
}
//DbURL generates InfluxDB server url
func (nmon *Nmon) DbURL() string {
return "http://" + nmon.Config.InfluxdbServer + ":" + nmon.Config.InfluxdbPort
}