-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh_mon_status.go
More file actions
44 lines (34 loc) · 1.04 KB
/
ssh_mon_status.go
File metadata and controls
44 lines (34 loc) · 1.04 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
package main
import (
"fmt"
"os/exec"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var ssh_mon_status = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "ssh_mon_status",
Help: "SSH Monitor Status and response time",
}, []string{"sshd_host"})
func measureCommandResponseTime(command string, args ...string) (float64, error) {
startTime := time.Now()
cmd := exec.Command(command, args...)
err := cmd.Run()
endTime := time.Now()
duration := endTime.Sub(startTime).Seconds()
return duration, err
}
func check_ssh_status() {
// username := "vagrant"
// host_ip := "192.168.16.128"
username := "testuser"
host_ip := "8.19.55.99"
ssh_id := username + "@" + host_ip
duration, err := measureCommandResponseTime("ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=30", ssh_id, "exit")
if err != nil {
ssh_mon_status.WithLabelValues(host_ip).Set(-1)
fmt.Printf("Command failed: %v\n", err)
} else {
ssh_mon_status.WithLabelValues(host_ip).Set(duration)
// fmt.Printf("Command response time: %.2f seconds\n", duration)
}
}