Skip to content

Instantly share code, notes, and snippets.

@youyo
Last active October 2, 2018 19:12
Show Gist options
  • Save youyo/03f55553773b14f4dad2102c229874eb to your computer and use it in GitHub Desktop.
Save youyo/03f55553773b14f4dad2102c229874eb to your computer and use it in GitHub Desktop.
proxy-server for conversion protocol from http to zabbix-tcp
package main
import (
"bufio"
"encoding/json"
"net"
"net/http"
"time"
"github.com/labstack/echo"
)
type (
ZabbixPacket struct {
Request string `json:"request"`
Data []Item `json:"data"`
}
Item struct {
Host string `json:"host"`
Key string `json:"key"`
Value string `json:"value"`
Clock int64 `json:"clock"`
}
)
func main() {
// run echo server
e := echo.New()
e.POST("/", SendData)
e.Start(":1323")
}
func SendData(c echo.Context) error {
// create zabbix-packet
item := new(Item)
item.Clock = time.Now().Unix()
_ = c.Bind(item)
packet := &ZabbixPacket{
Request: "sender data",
}
packet.Data = append(packet.Data, *item)
packetBytes, _ := json.Marshal(packet)
// connect to zabbix-server
conn, _ := net.Dial("tcp", "zabbix-server:10051")
defer conn.Close()
// send data
conn.Write(packetBytes)
// recieve response
message, _ := bufio.NewReader(conn).ReadString('\n')
// return response
return c.String(http.StatusOK, message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment