/
actiontech-zabbix-logmonitor.go
136 lines (117 loc) · 3.06 KB
/
actiontech-zabbix-logmonitor.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
package main
import (
"flag"
"fmt"
"github.com/AlekSi/zabbix-sender"
"github.com/hpcloud/tail"
"net"
"net/http"
"os"
"regexp"
"time"
"encoding/json"
"bytes"
)
var (
filename = flag.String("file", "", "-file /path/to/file")
regexpstring = flag.String("regexp", "", "-regexp string want to match")
zabbixhost = flag.String("zabbix_host", "", "-zabbix_host zabbix host name")
zabbixkey = flag.String("zabbix_key", "", "-zabbix_key zabbix item key")
zabbixserverhost = flag.String("zabbix_server_host", "", "-zabbix_server_host zabbix server host")
zabbixserverport = flag.String("zabbix_server_port", "10051", "-zabbix_server_port zabbix server port")
open_falcon = flag.Bool("open_falcon", false, "-open_falcon send to open-falcon")
version = flag.Bool("version", false, "-version print version")
Version string
)
type MetricValue struct {
Endpoint string `json:"endpoint"`
Metric string `json:"metric"`
Value int `json:"value"`
Step int `json:"step"`
Type string `json:"counterType"`
Tags string `json:"tags"`
Timestamp int64 `json:"timestamp"`
}
func send2zabbix(lineText chan string){
zbx_serv_conn_str := *zabbixserverhost + ":" + *zabbixserverport
data := map[string]interface{}{*zabbixkey: <-lineText}
di := zabbix_sender.MakeDataItems(data, *zabbixhost)
addr, _ := net.ResolveTCPAddr("tcp", zbx_serv_conn_str)
zabbix_sender.Send(addr, di)
}
func send2falcon(num chan int){
hostname, err := os.Hostname()
step :=60
ticker := time.NewTicker(time.Second * time.Duration(step)).C
value := 0
mvs := []*MetricValue{}
if err != nil {
//log.Error("call os.Hostname() fail:", err)
}
for{
select {
case recv := <- num:
value += recv
case <-ticker:
mvs = append(mvs,&MetricValue{
Endpoint: hostname,
Metric: *zabbixkey,
Value: value,
Step: step,
Type: "GAUGE",
Tags: "regexpstring=" + *regexpstring,
Timestamp: time.Now().Unix(),
})
go sendData(mvs)
value = 0
}
}
}
func sendData(data []*MetricValue){
bs, err := json.Marshal(data)
if err != nil {
//log .. nil, err
return
}
strUrl := "http://127.0.0.1:1988/v1/push"
_, err = http.Post(strUrl, "Content-Type: application/json", bytes.NewBuffer(bs))
if err != nil {
//log .. nil ,err
}
return
}
func main() {
flag.Parse()
if *version {
fmt.Println("version", Version)
os.Exit(1)
}
t, err := tail.TailFile(*filename, tail.Config{
Follow: true,
ReOpen: true,
Poll: true,
Location: &tail.SeekInfo{0, 2}})
if nil != err {
fmt.Println(err)
os.Exit(1)
}
num := make(chan int)
if *open_falcon {
go send2falcon(num)
}
lineText := make(chan string, 1)
for line := range t.Lines {
matched, err := regexp.MatchString(*regexpstring, line.Text)
if nil != err {
fmt.Println(err)
os.Exit(1)
}
if matched == true {
if *open_falcon {
num <- 1
}
lineText <- line.Text
go send2zabbix(lineText) //send2zabbix
}
}
}