-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (69 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"log"
"os"
syslog "github.com/RackSec/srslog"
"github.com/urfave/cli"
)
func writeMessage(message string, dest string, prot string, info string,
formatter syslog.Formatter) {
sysLog, err := syslog.Dial(prot, dest,
syslog.LOG_WARNING|syslog.LOG_DAEMON, info)
if err != nil {
log.Fatal(err)
}
sysLog.SetFormatter(formatter)
sysLog.Info(message)
fmt.Printf("sent message: %s\n in format: %s\n to: %s://%s\n", message, info, prot, dest)
}
func main() {
var format string
var message string
var dest string
var prot string
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "format, f",
Value: "rfc5424",
Usage: "Which RFC format should be used [rfc5424, rfc3164, comp]",
Destination: &format,
},
cli.StringFlag{
Name: "message, m",
Value: "DEFAULT MESSAGE",
Usage: "Message which should be sent",
Destination: &message,
},
cli.StringFlag{
Name: "dest, d",
Value: "localhost:1234",
Usage: "Destination Syslog endpoint [localhost:1234<]",
Destination: &dest,
},
cli.StringFlag{
Name: "prot, p",
Value: "udp",
Usage: "Network protocol which should be used [udp or tcp]",
Destination: &prot,
},
}
app.Action = func(c *cli.Context) error {
switch {
case format == "rfc5424":
writeMessage(message, dest, prot, "RFC5424", syslog.RFC5424Formatter)
case format == "rfc3164":
writeMessage(message, dest, prot, "RFC3164", syslog.RFC3164Formatter)
case format == "comp":
writeMessage(message, dest, prot, "COMPATIBILITY", syslog.DefaultFormatter)
default:
return cli.NewExitError("no valid format", 1)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}