-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (54 loc) · 1.86 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
)
// Main function
func main() {
//Initialize variable that holds status
var nrpeStatus NrpeStatus
//Call function that does all work with
checkNrpe(&nrpeStatus)
//Print status message and exit with correct code
// fmt.Println(nrpeStatus.Code)
fmt.Println(nrpeStatus.Message)
os.Exit(int(nrpeStatus.Code))
}
// This function does all work and sets the status message and code
func checkNrpe(nrpeStatus *NrpeStatus) {
//Set default return values
nrpeStatus.Message = "unknown"
nrpeStatus.Code = UNKNOWN
//Check if reboot-required file exists
var rebootRequiredFile = "/var/run/reboot-required"
if fileInfo, err := os.Stat(rebootRequiredFile); os.IsNotExist(err) {
//Reboot not required
nrpeStatus.Code = OK
nrpeStatus.Message = "Reboot not required."
} else {
//Check age of reboot-required. If older than two days, set status to CRITICAL, else set status to WARNING
var rebootRequiredFileAge = time.Now().Sub(fileInfo.ModTime())
if rebootRequiredFileAge.Hours() >= 48 {
nrpeStatus.Code = CRITICAL
} else {
nrpeStatus.Code = WARNING
}
//Get the reason why by reading the contents of reboot-required.pkgs
var rebootRequiredPkgs = rebootRequiredFile + ".pkgs"
if _, err := os.Stat(rebootRequiredPkgs); os.IsNotExist(err) {
//File didn't exist, reason is unknown
nrpeStatus.Message = fmt.Sprintf("System needs reboot! Reboot pending for %.0f hours. Responsible packages: unknown", rebootRequiredFileAge.Hours())
} else {
//Read content of file to get the reason
pkgs, err := ioutil.ReadFile(rebootRequiredPkgs)
if err != nil {
} else {
pkgsString := strings.Replace(string(pkgs), "\n", ", ", -1)
nrpeStatus.Message = fmt.Sprintf("System needs reboot! Reboot pending for %.0f hours. Responsible packages: %s", rebootRequiredFileAge.Hours(), pkgsString)
}
}
}
}