-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
160 lines (117 loc) · 3.1 KB
/
Copy pathmain.go
File metadata and controls
160 lines (117 loc) · 3.1 KB
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
159
160
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/ggiamarchi/http-check/logger"
"github.com/gin-gonic/gin"
cli "github.com/jawher/mow.cli"
yaml "gopkg.in/yaml.v2"
)
type check struct {
Name string `json:"name" yaml:"name"`
Command command `json:"command" yaml:"command"`
Status status `json:"status" yaml:"status"`
}
func (c *check) String() string {
return fmt.Sprintf("%+v", *c)
}
type command struct {
Executable string `json:"executable" yaml:"executable"`
Args []interface{} `json:"args" yaml:"args"`
}
func (c *command) String() string {
return fmt.Sprintf("%+v", *c)
}
type status struct {
Failure int `json:"failure" yaml:"failure"`
Success int `json:"success" yaml:"success"`
}
func (s *status) String() string {
return fmt.Sprintf("%+v", *s)
}
type appConfig struct {
Checks []check
Server struct {
Port int
}
}
func (c *appConfig) String() string {
return fmt.Sprintf("%+v", *c)
}
func main() {
app := cli.App("http-check", "HTTP Check exposes system commands as a single HTTP endpoint")
app.Command("server", "Run HTTP Check server", func(cmd *cli.Cmd) {
var configFile = cmd.StringOpt("c config", "/etc/http-check/http-check.yml", "HTTP Check YAML configuration file")
cmd.Action = func() {
logger.Init(false)
logger.Info("Starting HTTP Check server...")
appConfig := loadAppConfig(*configFile)
s := &http.Server{
Addr: fmt.Sprintf(":%d", appConfig.Server.Port),
Handler: api(appConfig),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
s.ListenAndServe()
}
})
app.Run(os.Args)
}
func api(appConfig *appConfig) *gin.Engine {
api := gin.New()
api.Use(logger.APILogger(), gin.Recovery())
v1 := api.Group("/v1")
checks := make(map[string]check)
for _, check := range appConfig.Checks {
checks[check.Name] = check
}
v1.GET("/check/:name", func(c *gin.Context) {
check := checks[c.Param("name")]
stdout, stderr, err := execCommand(check.Command.Executable, check.Command.Args...)
responseCode := check.Status.Success
errorMsg := ""
if err != nil {
logger.Info("error :: %s", err)
responseCode = check.Status.Failure
errorMsg = err.Error()
}
c.JSON(responseCode, gin.H{
"stdout": stdout,
"stderr": stderr,
"error": errorMsg,
})
})
return api
}
func loadAppConfig(file string) *appConfig {
data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
c := appConfig{}
err = yaml.Unmarshal([]byte(data), &c)
if err != nil {
panic(err)
}
return &c
}
func execCommand(command string, args ...interface{}) (string, string, error) {
fmtCommand := fmt.Sprintf(command, args...)
splitCommand := strings.Split(fmtCommand, " ")
logger.Info("Executing command :: %s :: with args :: %v => %s", command, args, fmtCommand)
cmdName := splitCommand[0]
cmdArgs := splitCommand[1:len(splitCommand)]
cmd := exec.Command(cmdName, cmdArgs...)
var stdout bytes.Buffer
cmd.Stdout = &stdout
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}