-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_exporter.go
164 lines (147 loc) · 6.02 KB
/
cron_exporter.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
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
161
162
163
164
package main
import (
"flag"
"os/exec"
"net/http"
"sync"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
const (
namespace = "cron" // For Prometheus metrics.
)
var (
listenAddress = flag.String("telemetry.address", ":9114", "Address on which to expose metrics.")
metricsPath = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
cronUsers = flag.String("cron.users", "ubuntu,user2,user3", "list of cron users")
syslogfile = flag.String("syslog.file", "/var/log/syslog", "Syslog file location")
)
// Exporter collects cron stats from machine of a specified user and exports them using
// the prometheus metrics package.
type Exporter struct {
CronUser string
mutex sync.RWMutex
totalCronJobs prometheus.Gauge
cronRunningStatus *prometheus.GaugeVec
}
// NewCronExporter returns an initialized Exporter.
func NewCronExporter(cronuser string) *Exporter {
return &Exporter{
CronUser: cronuser,
totalCronJobs: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "total_crons",
Help: "Total no of crons",
}),
cronRunningStatus: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "status",
Help: "cronjob last running status",
},
[]string{"user", "pattern", "readable", "command" ,"nextrun","previousrun"},
),
}
}
// Describe describes all the metrics ever exported by the cron exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.totalCronJobs.Describe(ch)
e.cronRunningStatus.Describe(ch)
}
func getCronScheduleFromPHP(cronString string) string {
<<<<<<< HEAD
phpcmd := "php ./cron_parser.php "+"'"+cronString+"'"
=======
phpcmd := "php ./CronSchedule.php "+"'"+cronString+"'"
>>>>>>> 8a018c8a8ea90a7328181a40026c4ec78f60e0c6
out, _ := exec.Command("bash", "-c", phpcmd).Output()
return string(out)
}
func grepCronLogStatus(cron_command string, cron_last_run string) string{
grepcmd := "grep -r CRON "+*syslogfile+ " | grep '"+cron_last_run+"'"+ " | grep '"+strings.TrimSpace(cron_command)+"'"
out, _ := exec.Command("bash", "-c", grepcmd).Output()
if string(out) != "" {
return "success"
}else{
return "failed"
}
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {
total_cron := 0
cron_users_list := strings.Split(*cronUsers, ",")
for _, user := range cron_users_list{
user = strings.TrimSpace(user)
cmd := "sudo /usr/bin/crontab -l -u " + user + " | grep -v '^#' | sed 's/^ *//;/^[*@0-9]/!d'"
output, _ := exec.Command("bash", "-c", cmd).Output()
if string(output) != ""{
crons := string(output)
crons = strings.TrimSpace(crons)
crons = strings.Replace(crons,"\n","|",-1)
cronSlice := strings.Split(crons, "|")
for _, cron := range cronSlice{
if cron != ""{
var cron_pattern string
var cron_command string
matches := strings.Fields(cron)
for _,val := range matches[:5]{
cron_pattern += string(val)+" "
}
for _, val1 := range matches[5:]{
cron_command += string(val1)+" "
}
cron_pattern = strings.TrimSpace(cron_pattern)
cron_command = strings.TrimSpace(cron_command)
cronruns := getCronScheduleFromPHP(cron_pattern)
cronrunsSlice := strings.Split(cronruns, "|")
humanReadable := cronrunsSlice[len(cronrunsSlice)-3]
cronNextRunTime := cronrunsSlice[len(cronrunsSlice)-2]
cronLastRunTime := cronrunsSlice[len(cronrunsSlice)-1]
var cronLastRun string
cronLastRunSlice := strings.Split(cronLastRunTime, " ")
cronLastRun = cronLastRunSlice[len(cronLastRunSlice)-3]+" "+cronLastRunSlice[len(cronLastRunSlice)-2]+" "+cronLastRunSlice[len(cronLastRunSlice)-1]
cron_status := grepCronLogStatus(cron_command, cronLastRun)
if cron_status == "success"{
e.cronRunningStatus.WithLabelValues(user, cron_pattern,humanReadable,cron_command,cronNextRunTime,cronLastRunTime).Set(float64(1))
}
if cron_status == "failed"{
e.cronRunningStatus.WithLabelValues(user, cron_pattern,humanReadable,cron_command,cronNextRunTime,cronLastRunTime).Set(float64(0))
}
}
}
total_cron = total_cron+ len(cronSlice)
}
}
e.totalCronJobs.Set(float64(total_cron))
return nil
}
// Collect fetches the stats of a user and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
if err := e.scrape(ch); err != nil {
log.Printf("Error scraping cron: %s", err)
}
e.totalCronJobs.Collect(ch)
e.cronRunningStatus.Collect(ch)
return
}
func main() {
flag.Parse()
exporter := NewCronExporter(*cronUsers)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Cronjob exporter</title></head>
<body>
<h1>Cronjob exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}