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 @@
/*
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 implement zabbix sender protocol for send metrics to zabbix.
package zabbix
import (
@ -44,7 +11,7 @@ import (
"time"
)
// Metric class
// Metric class.
type Metric struct {
Host string `json:"host"`
Key string `json:"key"`
@ -52,7 +19,7 @@ type Metric struct {
Clock int64 `json:"clock"`
}
// Metric class constructor
// Metric class constructor.
func NewMetric(host, key, value string, clock ...int64) *Metric {
m := new(Metric)
m.Host = host
@ -65,14 +32,14 @@ func NewMetric(host, key, value string, clock ...int64) *Metric {
return m
}
// Packet class
// Packet class.
type Packet struct {
Request string `json:"request"`
Data []*Metric `json:"data"`
Clock int64 `json:"clock"`
}
// Packet class cunstructor
// Packet class cunstructor.
func NewPacket(data []*Metric, clock ...int64) *Packet {
p := new(Packet)
p.Request = `sender data`
@ -84,21 +51,21 @@ func NewPacket(data []*Metric, clock ...int64) *Packet {
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 {
dataLen := make([]byte, 8)
jsonData, _ := json.Marshal(p)
binary.LittleEndian.PutUint32(dataLen, uint32(len(jsonData)))
JSONData, _ := json.Marshal(p)
binary.LittleEndian.PutUint32(dataLen, uint32(len(JSONData)))
return dataLen
}
// Sender class
// Sender class.
type Sender struct {
Host string
Port int
}
// Sender class constructor
// Sender class constructor.
func NewSender(host string, port int) *Sender {
s := new(Sender)
s.Host = host
@ -106,12 +73,12 @@ func NewSender(host string, port int) *Sender {
return s
}
// Method Sender class, return zabbix header
// Method Sender class, return zabbix header.
func (s Sender) getHeader() []byte {
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 {
// format: hostname:port
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
@ -127,11 +94,10 @@ func (s Sender) getTCPAddr() *net.TCPAddr {
return iaddr
}
// Method Sender class, make connection to uri
// Method Sender class, make connection to uri.
func (s Sender) connect() *net.TCPConn {
iaddr := s.getTCPAddr()
// Open connection to zabbix host
iaddr := s.getTCPAddr()
conn, err := net.DialTCP("tcp", nil, iaddr)
if err != nil {
@ -142,10 +108,9 @@ func (s Sender) connect() *net.TCPConn {
return conn
}
// Method Sender class, read data from connection
// Method Sender class, read data from connection.
func (s Sender) read(conn *net.TCPConn) []byte {
res := make([]byte, 1024)
res, err := ioutil.ReadAll(conn)
if err != nil {
fmt.Printf("Error whule receiving the data: %s", err.Error())
@ -155,18 +120,19 @@ func (s Sender) read(conn *net.TCPConn) []byte {
return res
}
// Method Sender class, send packet to zabbix
func (s Sender) Send(packet *Packet) {
// Method Sender class, send packet to zabbix.
func (s Sender) Send(packet *Packet) []byte {
conn := s.connect()
defer conn.Close()
dataPacket, _ := json.Marshal(packet)
// 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("BODY: %s\n", string(dataPacket))
*/
// Fill buffer
buffer := append(s.getHeader(), packet.DataLen()...)
@ -180,5 +146,8 @@ func (s Sender) Send(packet *Packet) {
}
res := s.read(conn)
/*
fmt.Printf("RESPONSE: %s\n", string(res))
*/
return res
}