-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (80 loc) · 1.88 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
package main
import (
"log"
"net/http"
"github.com/kardianos/service"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc/mgr"
)
var logger service.Logger
type program struct{}
const (
// Host name of the HTTP Server
host = "0.0.0.0"
// Port of the HTTP Server
port = "80"
// Cyber Ark PSM Name
windowsService = "Cyber-Ark Privileged Session Manager"
)
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}
func (p *program) run() {
// Do work here
http.HandleFunc("/", reportState)
err := http.ListenAndServe(host+":"+port, nil)
if err != nil {
log.Fatal("Error Starting the HTTP Server : ", err)
return
}
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
func reportState(w http.ResponseWriter, r *http.Request) {
manager, err := mgr.Connect()
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("FAIL"))
}
serviceInstance, err := manager.OpenService(windowsService)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("FAIL"))
} else {
serviceState, _ := serviceInstance.Query()
switch serviceState.State {
case windows.SERVICE_RUNNING:
w.WriteHeader(http.StatusOK)
w.Write([]byte("PASS"))
default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("FAIL"))
}
}
}
func main() {
svcConfig := &service.Config{
Name: "PSMSVCCheck",
DisplayName: "CyberArk PSM Service Check",
Description: "CyberArk PSM Service Check",
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}