-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatusd.go
122 lines (106 loc) · 2.61 KB
/
statusd.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"fmt"
MQTT "github.com/eclipse/paho.mqtt.golang"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
)
// report ifconfig.me/all.json
// IP address
// Wireguard address
// uptime
// ....all to MQTT
// ....as json!
var knt int
var hostname string
type status struct {
Ip_address string `json:"ip_address"`
Uptime string `json:"uptime"`
Wg_address string `json:"wg_address"`
Date string `json:"date"`
Hostname string `json:"hostname"`
}
var curr_status status
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
currentTime := time.Now()
fmt.Printf("%s: %s: %s\n", currentTime.Format(time.Stamp), msg.Topic(), msg.Payload())
do_an_update(client)
}
func main() {
knt = 0
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
hostname, _ = os.Hostname()
opts := MQTT.NewClientOptions().AddBroker("tcp://willymartini.com:1883")
opts.SetClientID("statusd-" + hostname)
opts.SetUsername("wmo")
opts.SetPassword("blowme")
opts.SetDefaultPublishHandler(f)
topic := "statusd/incoming"
opts.OnConnect = func(c MQTT.Client) {
if token := c.Subscribe(topic, 0, f); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
} else {
fmt.Printf("Connected to server\n")
}
go do_updates(client)
<-c
}
func do_updates(client MQTT.Client) {
for {
do_an_update(client)
time.Sleep(300 * time.Second)
}
}
func do_an_update(client MQTT.Client) {
update_status(&curr_status)
pubtopic := "statusd/" + hostname
b, err := json.Marshal(curr_status)
if err != nil {
fmt.Printf("Error: %s", err)
}
fmt.Println(string(b))
client.Publish(pubtopic, 0, true, string(b))
}
func update_status(current *status) {
current.Ip_address = fetch_ext_ipaddr()
cmd := exec.Command("uptime")
out, _ := cmd.CombinedOutput()
current.Uptime = string(out)
cmd = exec.Command("ifconfig", "-a")
out, _ = cmd.CombinedOutput()
fmt.Println(string(out))
cmd = exec.Command("hostname", "-i")
out, _ = cmd.CombinedOutput()
wg := string(out)
current.Wg_address = wg
current.Hostname, _ = os.Hostname()
currentTime := time.Now()
current.Date = currentTime.String()
}
func fetch_ext_ipaddr() string {
response, err := http.Get("http://ifconfig.me/ip")
if err != nil {
fmt.Printf("%s", err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
}
return string(contents)
}
return ""
}