Style fix.

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

@ -1,37 +1,4 @@
/* // 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 (
@ -44,7 +11,7 @@ import (
"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,14 +32,14 @@ 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`
@ -84,21 +51,21 @@ func NewPacket(data []*Metric, clock ...int64) *Packet {
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
@ -106,12 +73,12 @@ func NewSender(host string, port int) *Sender {
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)
@ -127,11 +94,10 @@ func (s Sender) getTCPAddr() *net.TCPAddr {
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
iaddr := s.getTCPAddr()
conn, err := net.DialTCP("tcp", nil, iaddr) conn, err := net.DialTCP("tcp", nil, iaddr)
if err != nil { if err != nil {
@ -142,10 +108,9 @@ func (s Sender) connect() *net.TCPConn {
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())
@ -155,18 +120,19 @@ func (s Sender) read(conn *net.TCPConn) []byte {
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("HEADER: % x (%s)\n", s.getHeader(), s.getHeader())
fmt.Printf("DATALEN: % x, %d byte\n", packet.DataLen(), len(packet.DataLen())) fmt.Printf("DATALEN: % x, %d byte\n", packet.DataLen(), len(packet.DataLen()))
fmt.Printf("BODY: %s\n", string(dataPacket)) fmt.Printf("BODY: %s\n", string(dataPacket))
*/
// Fill buffer // Fill buffer
buffer := append(s.getHeader(), packet.DataLen()...) buffer := append(s.getHeader(), packet.DataLen()...)
@ -180,5 +146,8 @@ func (s Sender) Send(packet *Packet) {
} }
res := s.read(conn) res := s.read(conn)
/*
fmt.Printf("RESPONSE: %s\n", string(res)) fmt.Printf("RESPONSE: %s\n", string(res))
*/
return res
} }