-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
158 lines (135 loc) · 3.18 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Quick and simple monitoring system
Usage:
jsonmon [-syslog] config.yml
jsonmon -version
Docs:
https://github.com/chillum/jsonmon/wiki
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"gopkg.in/yaml.v2"
)
// Version is the application version.
const Version = "3.1.15"
// This one is for internal use.
type ver struct {
App string `json:"jsonmon"`
Go string `json:"runtime"`
Os string `json:"os"`
Arch string `json:"arch"`
}
var version ver
// Global checks list. Need to share it with workers and Web UI.
var checks []Check
var mutex *sync.RWMutex
// Global started and last modified date for HTTP caching.
var modified string
var started string
var modHTML string
var modAngular string
var modJS string
var modCSS string
var useSyslog *bool
// The main loop.
func main() {
var err error
// Parse CLI args.
cliVersion := flag.Bool("version", false, "")
useSyslog = flag.Bool("syslog", false, "")
flag.Usage = func() {
fmt.Fprint(os.Stderr,
"Usage: jsonmon [-syslog] config.yml\n",
" jsonmon -version\n",
"----------------------------------------------\n",
"Docs: https://github.com/chillum/jsonmon/wiki\n")
os.Exit(1)
}
flag.Parse()
// -v for version.
version.App = Version
version.Go = runtime.Version()
version.Os = runtime.GOOS
version.Arch = runtime.GOARCH
if *cliVersion {
json, _ := json.Marshal(&version)
fmt.Println(string(json))
os.Exit(0)
}
// Help message if no arguments.
args := flag.Args()
if len(args) != 1 {
flag.Usage()
}
// Initialize system log.
if *useSyslog {
logs, err = logInit()
if err != nil {
*useSyslog = false
log(3, "Syslog failed, disabling: "+err.Error())
}
}
// Parse the config file or exit with error.
config, err := os.ReadFile(args[0])
if err != nil {
log(2, err.Error())
os.Exit(3)
}
err = yaml.Unmarshal(config, &checks)
if err != nil {
log(2, "invalid config at "+os.Args[1]+"\n"+err.Error())
os.Exit(3)
}
// Exit with return code 0 on kill.
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGTERM)
go func() {
<-done
os.Exit(0)
}()
// Run checks and init HTTP cache.
started = etag(time.Now())
modified = started
mutex = &sync.RWMutex{}
for i := range checks {
go checks[i].Run()
}
cacheHTML, _ := AssetInfo("index.html")
modHTML = cacheHTML.ModTime().UTC().Format(http.TimeFormat)
cacheAngular, _ := AssetInfo("angular.min.js")
modAngular = cacheAngular.ModTime().UTC().Format(http.TimeFormat)
cacheJS, _ := AssetInfo("app.js")
modJS = cacheJS.ModTime().UTC().Format(http.TimeFormat)
cacheCSS, _ := AssetInfo("main.css")
modCSS = cacheCSS.ModTime().UTC().Format(http.TimeFormat)
// Launch the Web server.
host := os.Getenv("HOST")
if host == "" {
host = "localhost"
}
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
listen := host + ":" + port
http.HandleFunc("/status", getChecks)
http.HandleFunc("/version", getVersion)
http.HandleFunc("/", getUI)
log(7, "Starting HTTP service at "+listen)
err = http.ListenAndServe(listen, nil)
if err != nil {
log(2, err.Error())
log(7, "Use HOST and PORT env variables to customize server settings")
}
os.Exit(4)
}