Style fix.

This commit is contained in:
Alexey Dubkov 2015-02-26 01:58:27 -08:00
parent c08d17f87e
commit 5a554e819e

147
zabbix.go

@ -1,50 +1,17 @@
/* // Package implement zabbix sender protocol for send metrics to zabbix.
Zabbix sender protocol defenition:
HEADER: ZBXD\x01, 5 byte
DATALEN: 8 byte, little-endian
DATA: JSON data
Request packet to zabbix host:
<HEADER><DATALEN>{
"request": "sender data",
"data": [
{
"host": "Host name 1",
"key": "item_key",
"value": "33",
"clock": 1381482894
},
{
"host": "Host name 2",
"key": "item_key",
"value": "55",
"clock": 1381482894
}
],
"clock": 1381482905
}
Response packet:
<HEADER><DATALEN>{
"response":"success",
"info":"Processed 2 Failed 0 Total 2 Seconds spent 0.002070"
}
*/
package zabbix package zabbix
import ( import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
"time" "time"
) )
// Metric class // Metric class.
type Metric struct { type Metric struct {
Host string `json:"host"` Host string `json:"host"`
Key string `json:"key"` Key string `json:"key"`
@ -52,7 +19,7 @@ type Metric struct {
Clock int64 `json:"clock"` Clock int64 `json:"clock"`
} }
// Metric class constructor // Metric class constructor.
func NewMetric(host, key, value string, clock ...int64) *Metric { func NewMetric(host, key, value string, clock ...int64) *Metric {
m := new(Metric) m := new(Metric)
m.Host = host m.Host = host
@ -65,120 +32,122 @@ func NewMetric(host, key, value string, clock ...int64) *Metric {
return m return m
} }
// Packet class // Packet class.
type Packet struct { type Packet struct {
Request string `json:"request"` Request string `json:"request"`
Data []*Metric `json:"data"` Data []*Metric `json:"data"`
Clock int64 `json:"clock"` Clock int64 `json:"clock"`
} }
// Packet class cunstructor // Packet class cunstructor.
func NewPacket(data []*Metric, clock ...int64) *Packet { func NewPacket(data []*Metric, clock ...int64) *Packet {
p := new(Packet) p := new(Packet)
p.Request = `sender data` p.Request = `sender data`
p.Data = data p.Data = data
// use current time, if `clock` is not specified // use current time, if `clock` is not specified
if p.Clock = time.Now().Unix(); len(clock) > 0 { if p.Clock = time.Now().Unix(); len(clock) > 0 {
p.Clock = int64(clock[0]) p.Clock = int64(clock[0])
} }
return p return p
} }
// DataLen. Packet class method, return 8 bytes with packet length in little endian order // DataLen Packet class method, return 8 bytes with packet length in little endian order.
func (p Packet) DataLen() []byte { func (p Packet) DataLen() []byte {
dataLen := make([]byte, 8) dataLen := make([]byte, 8)
jsonData, _ := json.Marshal(p) JSONData, _ := json.Marshal(p)
binary.LittleEndian.PutUint32(dataLen, uint32(len(jsonData))) binary.LittleEndian.PutUint32(dataLen, uint32(len(JSONData)))
return dataLen return dataLen
} }
// Sender class // Sender class.
type Sender struct { type Sender struct {
Host string Host string
Port int Port int
} }
// Sender class constructor // Sender class constructor.
func NewSender(host string, port int) *Sender { func NewSender(host string, port int) *Sender {
s := new(Sender) s := new(Sender)
s.Host = host s.Host = host
s.Port = port s.Port = port
return s return s
} }
// Method Sender class, return zabbix header // Method Sender class, return zabbix header.
func (s Sender) getHeader() []byte { func (s Sender) getHeader() []byte {
return []byte("ZBXD\x01") return []byte("ZBXD\x01")
} }
// Method Sender class, resolve uri by name:port // Method Sender class, resolve uri by name:port.
func (s Sender) getTCPAddr() *net.TCPAddr { func (s Sender) getTCPAddr() *net.TCPAddr {
// format: hostname:port // format: hostname:port
addr := fmt.Sprintf("%s:%d", s.Host, s.Port) addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
// Resolve hostname:port to ip:port // Resolve hostname:port to ip:port
iaddr, err := net.ResolveTCPAddr("tcp", addr) iaddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil { if err != nil {
fmt.Printf("Connection failed: %s", err.Error()) fmt.Printf("Connection failed: %s", err.Error())
os.Exit(1) os.Exit(1)
} }
return iaddr return iaddr
} }
// Method Sender class, make connection to uri // Method Sender class, make connection to uri.
func (s Sender) connect() *net.TCPConn { func (s Sender) connect() *net.TCPConn {
iaddr := s.getTCPAddr()
// Open connection to zabbix host // Open connection to zabbix host
conn, err := net.DialTCP("tcp", nil, iaddr) iaddr := s.getTCPAddr()
conn, err := net.DialTCP("tcp", nil, iaddr)
if err != nil { if err != nil {
fmt.Printf("Connection failed: %s", err.Error()) fmt.Printf("Connection failed: %s", err.Error())
os.Exit(1) os.Exit(1)
} }
return conn return conn
} }
// Method Sender class, read data from connection // Method Sender class, read data from connection.
func (s Sender) read(conn *net.TCPConn) []byte { func (s Sender) read(conn *net.TCPConn) []byte {
res := make([]byte, 1024) res := make([]byte, 1024)
res, err := ioutil.ReadAll(conn)
res, err := ioutil.ReadAll(conn)
if err != nil { if err != nil {
fmt.Printf("Error whule receiving the data: %s", err.Error()) fmt.Printf("Error whule receiving the data: %s", err.Error())
os.Exit(1) os.Exit(1)
} }
return res return res
} }
// Method Sender class, send packet to zabbix // Method Sender class, send packet to zabbix.
func (s Sender) Send(packet *Packet) { func (s Sender) Send(packet *Packet) []byte {
conn := s.connect()
conn := s.connect()
defer conn.Close() defer conn.Close()
dataPacket, _ := json.Marshal(packet) dataPacket, _ := json.Marshal(packet)
// Make zabbix header // Make zabbix header
fmt.Printf("HEADER: % x (%s)\n", s.getHeader(), s.getHeader()) /*
fmt.Printf("DATALEN: % x, %d byte\n", packet.DataLen(), len(packet.DataLen())) fmt.Printf("HEADER: % x (%s)\n", s.getHeader(), s.getHeader())
fmt.Printf("BODY: %s\n", string(dataPacket)) fmt.Printf("DATALEN: % x, %d byte\n", packet.DataLen(), len(packet.DataLen()))
fmt.Printf("BODY: %s\n", string(dataPacket))
*/
// Fill buffer // Fill buffer
buffer := append(s.getHeader(), packet.DataLen()...) buffer := append(s.getHeader(), packet.DataLen()...)
buffer = append(buffer, dataPacket...) buffer = append(buffer, dataPacket...)
// Sent packet to zabbix // Sent packet to zabbix
_, err := conn.Write(buffer) _, err := conn.Write(buffer)
if err != nil { if err != nil {
fmt.Printf("Error while sending the data: %s", err.Error()) fmt.Printf("Error while sending the data: %s", err.Error())
os.Exit(1) os.Exit(1)
} }
res := s.read(conn) res := s.read(conn)
fmt.Printf("RESPONSE: %s\n", string(res)) /*
fmt.Printf("RESPONSE: %s\n", string(res))
*/
return res
} }