-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (86 loc) · 2.9 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
package main
import (
"github.com/streadway/amqp"
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func read_in_stdin(debug bool) *bufio.Scanner {
// massive copy and paste
// https://flaviocopes.com/go-shell-pipes/
info, err := os.Stdin.Stat()
failOnError(err, "Failed to connect to stdin")
if debug {
fmt.Println("stdin buffer size = " + strconv.FormatInt(info.Size(), 10))
}
// removed the size test as this can return nothing but 0 on some OS's.
// if info.Mode()&os.ModeCharDevice != 0 || info.Size() <= 0 {
if info.Mode()&os.ModeCharDevice == os.ModeCharDevice {
fmt.Println("The command is intended to work with pipes.")
fmt.Println("Usage: echo mytext | stdin2rabbitmq")
os.Exit(1)
}
// https://medium.com/golangspec/in-depth-introduction-to-bufio-scanner-in-golang-55483bb689b4
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
//for scanner.Scan() {
// fmt.Println(scanner.Text())
//}
return scanner
}
func post_to_rabbitmq(debug bool, host string, payload string, port string, queue string, rabbituser string, rabbitpass string) {
// http://www.agiratech.com/message-queues-golang-rabbitmq/
if debug {
fmt.Println("amqp://" + rabbituser + ":" + rabbitpass + "@" + host + ":" + port + "/")
}
conn, err := amqp.Dial("amqp://" + rabbituser + ":" + rabbitpass + "@" + host + ":" + port + "/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
q, err := ch.QueueDeclare(
queue, // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare a queue")
body := payload
err = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
if debug {
log.Printf(" [x] Sent %s", body)
}
failOnError(err, "Failed to publish a message")
}
func main() {
debug := flag.Bool("debug", false, "turn on debug messages, default false")
host := flag.String("host", "localhost", "the rabbitmq hostname, default: localhost")
port := flag.String("port", "5672", "the rabbitmq amqp port, default: 5672")
queue := flag.String("queue", "logs", "the name of the rabbitmq queue, default: logs")
rabbituser := flag.String("user", "guest", "the rabbitmq username, default: guest")
rabbitpass := flag.String("pass", "guest", "the rabbitmq password, default: guest")
flag.Parse()
log_line := read_in_stdin(*debug)
for log_line.Scan() {
post_to_rabbitmq(*debug, *host, log_line.Text(), *port, *queue, *rabbituser, *rabbitpass)
}
}