syslog writing works
This commit is contained in:
@@ -9,6 +9,7 @@ RUN go build -a -installsuffix nocgo -o ma main.go
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
ENV MA_CONF ""
|
ENV MA_CONF ""
|
||||||
|
ENV MQTT_PASSWORD ""
|
||||||
|
|
||||||
COPY --from=builder /go/src/ma ./
|
COPY --from=builder /go/src/ma ./
|
||||||
ENTRYPOINT ["./ma"]
|
ENTRYPOINT ["./ma"]
|
||||||
|
|||||||
@@ -12,5 +12,13 @@
|
|||||||
"snmp",
|
"snmp",
|
||||||
"MainsCnt/#",
|
"MainsCnt/#",
|
||||||
"cem/#"
|
"cem/#"
|
||||||
]
|
],
|
||||||
|
"syslog": {
|
||||||
|
"enable": "true",
|
||||||
|
"network": "udp",
|
||||||
|
"server": "172.20.0.10:514",
|
||||||
|
"facility": "local0",
|
||||||
|
"severity": "info",
|
||||||
|
"tag": "mqtt-archiver"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,5 +12,13 @@
|
|||||||
"snmp",
|
"snmp",
|
||||||
"MainsCnt/#",
|
"MainsCnt/#",
|
||||||
"cem/#"
|
"cem/#"
|
||||||
]
|
],
|
||||||
|
"syslog": {
|
||||||
|
"enable": "false",
|
||||||
|
"network": "udp",
|
||||||
|
"server": "syslog-server:514",
|
||||||
|
"facility": "local0",
|
||||||
|
"severity": "info",
|
||||||
|
"tag": "mqtt-archiver"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Time time.Time
|
Time time.Time `json:"time"`
|
||||||
Topic string
|
Topic string `json:"topic"`
|
||||||
Payload string
|
Payload string `json:"payload"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitArchiver() {
|
func InitArchiver() {
|
||||||
log.Printf("Archiver initializing")
|
log.Printf("Archiver initializing")
|
||||||
|
InitSyslog()
|
||||||
}
|
}
|
||||||
|
|
||||||
func InputArchiver() {
|
func InputArchiver() {
|
||||||
@@ -25,4 +26,6 @@ func InputArchiver() {
|
|||||||
|
|
||||||
func handleMessage(message Message) {
|
func handleMessage(message Message) {
|
||||||
log.Printf("Archiving Timestamp: %s, Topic: %s, Payload: %s", message.Time, message.Topic, message.Payload)
|
log.Printf("Archiving Timestamp: %s, Topic: %s, Payload: %s", message.Time, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
WriteSyslog(message)
|
||||||
}
|
}
|
||||||
|
|||||||
115
src/ma/archiver/syslog.go
Normal file
115
src/ma/archiver/syslog.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package archiver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"log/syslog"
|
||||||
|
"ma/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var syslogWriter *syslog.Writer
|
||||||
|
|
||||||
|
func InitSyslog() {
|
||||||
|
if config.Config.Syslog.Enable == "true" {
|
||||||
|
// Parse facility
|
||||||
|
facility := parseFacility(config.Config.Syslog.Facility)
|
||||||
|
|
||||||
|
// Parse severity
|
||||||
|
severity := parseSeverity(config.Config.Syslog.Severity)
|
||||||
|
|
||||||
|
// Combine to priority
|
||||||
|
priority := facility | severity
|
||||||
|
|
||||||
|
var err error
|
||||||
|
syslogWriter, err = syslog.Dial(
|
||||||
|
config.Config.Syslog.Network,
|
||||||
|
config.Config.Syslog.Server,
|
||||||
|
priority,
|
||||||
|
config.Config.Syslog.Tag,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to connect to syslog server: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Syslog connection established: %s://%s", config.Config.Syslog.Network, config.Config.Syslog.Server)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteSyslog(message Message) {
|
||||||
|
if syslogWriter != nil {
|
||||||
|
jsonData, err := json.Marshal(message)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to marshal message to JSON: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to syslog based on configured severity
|
||||||
|
switch config.Config.Syslog.Severity {
|
||||||
|
case "emerg":
|
||||||
|
syslogWriter.Emerg(string(jsonData))
|
||||||
|
case "alert":
|
||||||
|
syslogWriter.Alert(string(jsonData))
|
||||||
|
case "crit":
|
||||||
|
syslogWriter.Crit(string(jsonData))
|
||||||
|
case "err":
|
||||||
|
syslogWriter.Err(string(jsonData))
|
||||||
|
case "warning":
|
||||||
|
syslogWriter.Warning(string(jsonData))
|
||||||
|
case "notice":
|
||||||
|
syslogWriter.Notice(string(jsonData))
|
||||||
|
case "info":
|
||||||
|
syslogWriter.Info(string(jsonData))
|
||||||
|
case "debug":
|
||||||
|
syslogWriter.Debug(string(jsonData))
|
||||||
|
default:
|
||||||
|
syslogWriter.Info(string(jsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseFacility(facility string) syslog.Priority {
|
||||||
|
facilities := map[string]syslog.Priority{
|
||||||
|
"kern": syslog.LOG_KERN,
|
||||||
|
"user": syslog.LOG_USER,
|
||||||
|
"mail": syslog.LOG_MAIL,
|
||||||
|
"daemon": syslog.LOG_DAEMON,
|
||||||
|
"auth": syslog.LOG_AUTH,
|
||||||
|
"syslog": syslog.LOG_SYSLOG,
|
||||||
|
"lpr": syslog.LOG_LPR,
|
||||||
|
"news": syslog.LOG_NEWS,
|
||||||
|
"uucp": syslog.LOG_UUCP,
|
||||||
|
"cron": syslog.LOG_CRON,
|
||||||
|
"authpriv": syslog.LOG_AUTHPRIV,
|
||||||
|
"ftp": syslog.LOG_FTP,
|
||||||
|
"local0": syslog.LOG_LOCAL0,
|
||||||
|
"local1": syslog.LOG_LOCAL1,
|
||||||
|
"local2": syslog.LOG_LOCAL2,
|
||||||
|
"local3": syslog.LOG_LOCAL3,
|
||||||
|
"local4": syslog.LOG_LOCAL4,
|
||||||
|
"local5": syslog.LOG_LOCAL5,
|
||||||
|
"local6": syslog.LOG_LOCAL6,
|
||||||
|
"local7": syslog.LOG_LOCAL7,
|
||||||
|
}
|
||||||
|
|
||||||
|
if f, ok := facilities[facility]; ok {
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
return syslog.LOG_LOCAL0 // Default
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseSeverity(severity string) syslog.Priority {
|
||||||
|
severities := map[string]syslog.Priority{
|
||||||
|
"emerg": syslog.LOG_EMERG,
|
||||||
|
"alert": syslog.LOG_ALERT,
|
||||||
|
"crit": syslog.LOG_CRIT,
|
||||||
|
"err": syslog.LOG_ERR,
|
||||||
|
"warning": syslog.LOG_WARNING,
|
||||||
|
"notice": syslog.LOG_NOTICE,
|
||||||
|
"info": syslog.LOG_INFO,
|
||||||
|
"debug": syslog.LOG_DEBUG,
|
||||||
|
}
|
||||||
|
|
||||||
|
if s, ok := severities[severity]; ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return syslog.LOG_INFO // Default
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "encoding/json"
|
import (
|
||||||
import "log"
|
"encoding/json"
|
||||||
import "os"
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type HandlerConfigT struct {
|
type HandlerConfigT struct {
|
||||||
Attributes map[string]string `json:"attributes"`
|
Attributes map[string]string `json:"attributes"`
|
||||||
@@ -17,11 +19,18 @@ type ConfigT struct {
|
|||||||
} `json:"mqtt"`
|
} `json:"mqtt"`
|
||||||
IncludeTopics []string `json:"includeTopics"`
|
IncludeTopics []string `json:"includeTopics"`
|
||||||
ExcludeTopics []string `json:"excludeTopics"`
|
ExcludeTopics []string `json:"excludeTopics"`
|
||||||
|
Syslog struct {
|
||||||
|
Enable string `json:"enable"`
|
||||||
|
Network string `json:"network"`
|
||||||
|
Server string `json:"server"`
|
||||||
|
Facility string `json:"facility"`
|
||||||
|
Severity string `json:"severity"`
|
||||||
|
Tag string `json:"tag"`
|
||||||
|
} `json:"syslog"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config ConfigT
|
var Config ConfigT
|
||||||
|
|
||||||
|
|
||||||
func LoadConfiguration() {
|
func LoadConfiguration() {
|
||||||
err := json.Unmarshal([]byte(os.Getenv("MA_CONF")), &Config)
|
err := json.Unmarshal([]byte(os.Getenv("MA_CONF")), &Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -30,4 +39,3 @@ func LoadConfiguration() {
|
|||||||
|
|
||||||
Config.Mqtt.Password = os.Getenv("MQTT_PASSWORD")
|
Config.Mqtt.Password = os.Getenv("MQTT_PASSWORD")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package mqtt
|
package mqtt
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
import "strings"
|
"fmt"
|
||||||
import "fmt"
|
"log"
|
||||||
import MQTT "github.com/eclipse/paho.mqtt.golang"
|
"strings"
|
||||||
import "github.com/google/uuid"
|
|
||||||
import "crypto/tls"
|
"crypto/tls"
|
||||||
import "ma/config"
|
"ma/config"
|
||||||
import "ma/counter"
|
"ma/counter"
|
||||||
|
|
||||||
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Topic string
|
Topic string
|
||||||
@@ -57,7 +61,6 @@ func onReconnecting(client MQTT.Client, clientOpts *MQTT.ClientOptions) {
|
|||||||
log.Println("Oops, connection lost, already reconnecting ...")
|
log.Println("Oops, connection lost, already reconnecting ...")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func onConnect(client MQTT.Client) {
|
func onConnect(client MQTT.Client) {
|
||||||
for _, topic := range config.Config.IncludeTopics {
|
for _, topic := range config.Config.IncludeTopics {
|
||||||
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
||||||
@@ -68,9 +71,7 @@ func onConnect(client MQTT.Client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func outputDispatcher(client MQTT.Client) {
|
func outputDispatcher(client MQTT.Client) {
|
||||||
for {
|
for message := range OutputChannel {
|
||||||
select {
|
|
||||||
case message := <- OutputChannel:
|
|
||||||
log.Printf("Message arrived in outputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
|
log.Printf("Message arrived in outputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
|
||||||
if token := client.Publish(message.Topic, 0, false, message.Payload); token.Wait() && token.Error() != nil {
|
if token := client.Publish(message.Topic, 0, false, message.Payload); token.Wait() && token.Error() != nil {
|
||||||
log.Printf("Unable to publish, error %s", token.Error())
|
log.Printf("Unable to publish, error %s", token.Error())
|
||||||
@@ -78,7 +79,6 @@ func outputDispatcher(client MQTT.Client) {
|
|||||||
log.Println("Successfully published")
|
log.Println("Successfully published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func StartMqttClient() {
|
func StartMqttClient() {
|
||||||
broker := config.Config.Mqtt.Broker
|
broker := config.Config.Mqtt.Broker
|
||||||
@@ -152,4 +152,3 @@ func TopicMatchesSubscription(topic, subscription string) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user