-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnomachinestatus.go
49 lines (40 loc) · 1.08 KB
/
nomachinestatus.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
package main
import (
"errors"
"os"
"github.com/mitchellh/go-ps"
)
type nomachineStatus struct {
HostName string `json:"host_name,omitempty"`
NoMachineRunning bool `json:"no_machine_running,omitempty"`
ClientAttached bool `json:"client_attached,omitempty"`
}
func getFirstProcessByName(name string) (int, error) {
processes, err := ps.Processes()
if err != nil {
return -1, err
}
for _, process := range processes {
if process.Executable() == name {
return process.Pid(), nil
}
}
return -1, errors.New("Could not find process " + name)
}
// getStatus returns a structure containing information on the status of NoMachine
func getStatus() (nomachineStatus, error) {
hostName, err := os.Hostname()
if err != nil {
return nomachineStatus{}, err
}
status := nomachineStatus{HostName: hostName}
noMachinePid, _ := getFirstProcessByName("nxserver.bin")
noMachineClientPid, _ := getFirstProcessByName("nxexec")
if noMachinePid >= 0 {
status.NoMachineRunning = true
}
if noMachineClientPid >= 0 {
status.ClientAttached = true
}
return status, nil
}