Compare commits
No commits in common. "3c6a95ec4fdc345b48c4e0e5f5c87d48d3fc40b5" and "a5c216792175f86dd4da18e7659d017243def041" have entirely different histories.
3c6a95ec4f
...
a5c2167921
97
zabbix.go
97
zabbix.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,7 +21,10 @@ type Metric struct {
|
|||||||
|
|
||||||
// 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 := &Metric{Host: host, Key: key, Value: value}
|
m := new(Metric)
|
||||||
|
m.Host = host
|
||||||
|
m.Key = key
|
||||||
|
m.Value = value
|
||||||
// use current time, if `clock` is not specified
|
// use current time, if `clock` is not specified
|
||||||
if m.Clock = time.Now().Unix(); len(clock) > 0 {
|
if m.Clock = time.Now().Unix(); len(clock) > 0 {
|
||||||
m.Clock = int64(clock[0])
|
m.Clock = int64(clock[0])
|
||||||
@ -37,7 +41,9 @@ type Packet struct {
|
|||||||
|
|
||||||
// Packet class cunstructor.
|
// Packet class cunstructor.
|
||||||
func NewPacket(data []*Metric, clock ...int64) *Packet {
|
func NewPacket(data []*Metric, clock ...int64) *Packet {
|
||||||
p := &Packet{Request: `sender data`, Data: data}
|
p := new(Packet)
|
||||||
|
p.Request = `sender 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])
|
||||||
@ -46,7 +52,7 @@ func NewPacket(data []*Metric, clock ...int64) *Packet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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)))
|
||||||
@ -61,89 +67,67 @@ type Sender struct {
|
|||||||
|
|
||||||
// Sender class constructor.
|
// Sender class constructor.
|
||||||
func NewSender(host string, port int) *Sender {
|
func NewSender(host string, port int) *Sender {
|
||||||
s := &Sender{Host: host, Port: port}
|
s := new(Sender)
|
||||||
|
s.Host = host
|
||||||
|
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() (iaddr *net.TCPAddr, err error) {
|
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 {
|
||||||
err = fmt.Errorf("Connection failed: %s", err.Error())
|
fmt.Printf("Connection failed: %s", err.Error())
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return iaddr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Sender class, make connection to uri.
|
// Method Sender class, make connection to uri.
|
||||||
func (s *Sender) connect() (conn *net.TCPConn, err error) {
|
func (s Sender) connect() *net.TCPConn {
|
||||||
|
|
||||||
type DialResp struct {
|
|
||||||
Conn *net.TCPConn
|
|
||||||
Error error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open connection to zabbix host
|
// Open connection to zabbix host
|
||||||
iaddr, err := s.getTCPAddr()
|
iaddr := s.getTCPAddr()
|
||||||
|
conn, err := net.DialTCP("tcp", nil, iaddr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
fmt.Printf("Connection failed: %s", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// dial tcp and handle timeouts
|
return conn
|
||||||
ch := make(chan DialResp)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err = net.DialTCP("tcp", nil, iaddr)
|
|
||||||
ch <- DialResp{Conn: conn, Error: err}
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
err = fmt.Errorf("Connection Timeout")
|
|
||||||
case resp := <-ch:
|
|
||||||
if resp.Error != nil {
|
|
||||||
err = resp.Error
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
conn = resp.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Sender class, read data from connection.
|
// Method Sender class, read data from connection.
|
||||||
func (s *Sender) read(conn *net.TCPConn) (res []byte, err error) {
|
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 {
|
||||||
err = fmt.Errorf("Error whule receiving the data: %s", err.Error())
|
fmt.Printf("Error whule receiving the data: %s", err.Error())
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Sender class, send packet to zabbix.
|
// Method Sender class, send packet to zabbix.
|
||||||
func (s *Sender) Send(packet *Packet) (res []byte, err error) {
|
func (s Sender) Send(packet *Packet) []byte {
|
||||||
conn, err := s.connect()
|
conn := s.connect()
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
dataPacket, _ := json.Marshal(packet)
|
dataPacket, _ := json.Marshal(packet)
|
||||||
|
|
||||||
|
// 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()))
|
||||||
@ -155,16 +139,15 @@ func (s *Sender) Send(packet *Packet) (res []byte, err error) {
|
|||||||
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 {
|
||||||
err = fmt.Errorf("Error while sending the data: %s", err.Error())
|
fmt.Printf("Error while sending the data: %s", err.Error())
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err = s.read(conn)
|
res := s.read(conn)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
fmt.Printf("RESPONSE: %s\n", string(res))
|
fmt.Printf("RESPONSE: %s\n", string(res))
|
||||||
*/
|
*/
|
||||||
return
|
return res
|
||||||
}
|
}
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
package zabbix
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
const (
|
|
||||||
hostname = `somehost.com`
|
|
||||||
zabbixhost = `172.30.30.30`
|
|
||||||
zabbixport = 1234
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSend(t *testing.T) {
|
|
||||||
sender := NewSender(zabbixhost, zabbixport)
|
|
||||||
|
|
||||||
metrics := []*Metric{NewMetric(hostname, `key`, `value`)}
|
|
||||||
_, err := sender.Send(NewPacket(metrics))
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Error("sending should have failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("error: %v", err.Error())
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user