-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmautic_exporter.go
324 lines (281 loc) · 12.8 KB
/
mautic_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"flag"
"fmt"
"os"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
const (
namespace = "mautic"
)
//This is my collector metrics
type mauticCollector struct {
numEmailsSentMetric *prometheus.Desc
numLeadsMetric *prometheus.Desc
numAnonymousMetric *prometheus.Desc
numEmailsMetric *prometheus.Desc
numCampaignsMetric *prometheus.Desc
numSegmentsMetric *prometheus.Desc
numPageHitsMetric *prometheus.Desc
numWebhookQueueMetric *prometheus.Desc
numMessageQueueMetric *prometheus.GaugeVec
numLeadEventFailedMetric *prometheus.Desc
numCampaignEventsMetric *prometheus.GaugeVec
numNotificationsMetric *prometheus.Desc
numPageRedirectMetric *prometheus.Desc
numLeadsinCampaignMetric *prometheus.GaugeVec
numLeadsinSegmentMetric *prometheus.GaugeVec
numChannelEmailSentMetric *prometheus.GaugeVec
numChannelEmailReadMetric *prometheus.GaugeVec
dbHost string
dbName string
dbUser string
dbPass string
dbTablePrefix string
}
//This is a constructor for my mauticCollector struct
func newMauticCollector(host string, dbname string, username string, pass string, tablePrefix string) *mauticCollector {
return &mauticCollector{
numEmailsSentMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "emails_sent_total"),
"Shows the number of emails sent in Mautic (MAX value)",
nil, nil,
),
numLeadsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "leads_total"),
"Shows the number of leads in Mautic",
nil, nil,
),
numAnonymousMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "anonymous_total"),
"Shows the number of anonymous in Mautic",
nil, nil,
),
numEmailsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "emails_total"),
"Shows the number of emails in Mautic",
nil, nil,
),
numCampaignsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "campaigns_total"),
"Shows the number of campaigns in Mautic",
nil, nil,
),
numSegmentsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "segments_total"),
"Shows the number of segments in Mautic",
nil, nil,
),
numPageHitsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "page_hits_total"),
"Shows the number of page hits in Mautic (MAX value)",
nil, nil,
),
numWebhookQueueMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "webhook_total"),
"Shows the number of webhook in Mautic's queue",
nil, nil,
),
numMessageQueueMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "queue_total",
Help: "Shows the number of message in Mautic's queue",
},
[]string{"type"},
),
numCampaignEventsMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "campaign_events_total",
Help: "Shows the number of campaign events in Mautic",
},
[]string{"type"},
),
numLeadEventFailedMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "lead_event_failed_total"),
"Shows the number of failed events on leads",
nil, nil,
),
numNotificationsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "notifications_total"),
"Shows the number of notifications in Mautic",
nil, nil,
),
numPageRedirectMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "page_redirect_total"),
"Shows the number of page redirects in Mautic (MAX value)",
nil, nil,
),
numLeadsinCampaignMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "leads_in_campaign_total",
Help: "Shows the number of leads in a campaign",
},
[]string{"type"},
),
numLeadsinSegmentMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "leads_in_segment_total",
Help: "Shows the number of leads in a segment",
},
[]string{"type"},
),
numChannelEmailSentMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "channel_email_sent_total",
Help: "Shows the number of emails sent in each email channel",
},
[]string{"type"},
),
numChannelEmailReadMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "channel_email_read_total",
Help: "Shows the number of emails read in each email channel",
},
[]string{"type"},
),
dbHost: host,
dbName: dbname,
dbUser: username,
dbPass: pass,
dbTablePrefix: tablePrefix,
}
}
//Describe method is required for a prometheus.Collector type
func (collector *mauticCollector) Describe(ch chan<- *prometheus.Desc) {
//We set the metrics
ch <- collector.numEmailsSentMetric
ch <- collector.numLeadsMetric
ch <- collector.numAnonymousMetric
ch <- collector.numEmailsMetric
ch <- collector.numCampaignsMetric
ch <- collector.numSegmentsMetric
ch <- collector.numPageHitsMetric
ch <- collector.numWebhookQueueMetric
ch <- collector.numLeadEventFailedMetric
ch <- collector.numNotificationsMetric
ch <- collector.numPageRedirectMetric
collector.numMessageQueueMetric.Describe(ch)
collector.numCampaignEventsMetric.Describe(ch)
collector.numLeadsinCampaignMetric.Describe(ch)
collector.numLeadsinSegmentMetric.Describe(ch)
collector.numChannelEmailSentMetric.Describe(ch)
collector.numChannelEmailReadMetric.Describe(ch)
}
//Collect method is required for a prometheus.Collector type
func (collector *mauticCollector) Collect(ch chan<- prometheus.Metric) {
//We run DB queries here to retrieve the metrics we care about
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", collector.dbUser, collector.dbPass, collector.dbHost, collector.dbName)
db, err := sql.Open("mysql", dsn)
if err != nil {
fmt.Fprintf(os.Stderr, "Error connecting to database: %s ...\n", err)
os.Exit(1)
}
defer db.Close()
//select max(id) from email_stats
queryNumEmailsSentMetric := fmt.Sprintf("select max(id) from %semail_stats;", collector.dbTablePrefix)
mtQueryCounter(db, ch, collector.numEmailsSentMetric, queryNumEmailsSentMetric)
//select count(id) as numLeads from leads where date_identified is not null
queryNumLeadsMetric := fmt.Sprintf("select count(id) as numLeads from %sleads where date_identified is not null;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numLeadsMetric, queryNumLeadsMetric)
//select count(id) as numAnonymous from leads where date_identified is null
queryNumAnonymousMetric := fmt.Sprintf("select count(id) as numAnonymous from %sleads where date_identified is null;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numAnonymousMetric, queryNumAnonymousMetric)
//select count(*) as numEmails from emails;
queryNumEmailsMetric := fmt.Sprintf("select count(*) as numEmails from %semails;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numEmailsMetric, queryNumEmailsMetric)
//select count(*) as numCampaigns from campaigns;
queryNumCampaignsMetric := fmt.Sprintf("select count(*) as numCampaigns from %scampaigns;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numCampaignsMetric, queryNumCampaignsMetric)
//select count(*) as numCampaigns from campaigns;
queryNumSegmentsMetric := fmt.Sprintf("select count(*) as numSegments from %slead_lists;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numSegmentsMetric, queryNumSegmentsMetric)
//select max(id) from page_hits;
queryNumPageHitsMetric := fmt.Sprintf("select max(id) from %spage_hits;", collector.dbTablePrefix)
mtQueryCounter(db, ch, collector.numPageHitsMetric, queryNumPageHitsMetric)
//select count(*) as numWebhookQueue from webhook_queue;
queryNumWebhookQueueMetric := fmt.Sprintf("select count(*) as numWebhookQueue from %swebhook_queue;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numWebhookQueueMetric, queryNumWebhookQueueMetric)
//select status as label, count(*) as value from message_queue group by status;
queryNumMessageQueueMetric := fmt.Sprintf("select status as label, count(*) as value from %smessage_queue group by status;", collector.dbTablePrefix)
mtQueryGaugeVec(db, ch, collector.numMessageQueueMetric, queryNumMessageQueueMetric)
//select type as label, count(*) as value from campaign_events GROUP BY type;
queryNumCampaignEventsMetric := fmt.Sprintf("select type as label, count(*) as value from %scampaign_events group by type;", collector.dbTablePrefix)
mtQueryGaugeVec(db, ch, collector.numCampaignEventsMetric, queryNumCampaignEventsMetric)
//select count(*) from campaign_lead_event_failed_log;
queryNumLeadEventFailedMetric := fmt.Sprintf("select count(*) from %scampaign_lead_event_failed_log;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numLeadEventFailedMetric, queryNumLeadEventFailedMetric)
//select count(*) from notifications;
queryNumNotificationsMetric := fmt.Sprintf("select count(*) from %snotifications;", collector.dbTablePrefix)
mtQueryGauge(db, ch, collector.numNotificationsMetric, queryNumNotificationsMetric)
//select max(id) from page_redirects;
queryNumPageRedirectMetric := fmt.Sprintf("select max(id) from %spage_redirects;", collector.dbTablePrefix)
mtQueryCounter(db, ch, collector.numPageRedirectMetric, queryNumPageRedirectMetric)
//select campaign_id as label, count(*) as value from campaign_leads where manually_removed = 0 group by campaign_id;
queryNumLeadsinCampaignMetric := fmt.Sprintf("select campaign_id as label, count(*) as value from %scampaign_leads where manually_removed = 0 group by campaign_id;", collector.dbTablePrefix)
mtQueryGaugeVec(db, ch, collector.numLeadsinCampaignMetric, queryNumLeadsinCampaignMetric)
//select leadlist_id as label, count(*) as value from lead_lists_leads group by leadlist_id ;
queryNumLeadsinSegmentMetric := fmt.Sprintf("select leadlist_id as label, count(*) as value from %slead_lists_leads group by leadlist_id;", collector.dbTablePrefix)
mtQueryGaugeVec(db, ch, collector.numLeadsinSegmentMetric, queryNumLeadsinSegmentMetric)
queryNumChannelEmailSentMetric := fmt.Sprintf("select `id` as label, `sent_count` as value from `%semails` WHERE `is_published` = 1;", collector.dbTablePrefix)
mtQueryGaugeVec(db, ch, collector.numChannelEmailSentMetric, queryNumChannelEmailSentMetric)
queryNumChannelEmailReadMetric := fmt.Sprintf("select `id` as label, `read_count` as value from `%semails` WHERE `is_published` = 1;", collector.dbTablePrefix)
mtQueryGaugeVec(db, ch, collector.numChannelEmailReadMetric, queryNumChannelEmailReadMetric)
}
func mtQueryCounter(db *sql.DB, ch chan<- prometheus.Metric, desc *prometheus.Desc, mysqlRequest string) {
var value float64
var err = db.QueryRow(mysqlRequest).Scan(&value)
if err != nil {
log.Fatal(err)
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, value)
}
func mtQueryGauge(db *sql.DB, ch chan<- prometheus.Metric, desc *prometheus.Desc, mysqlRequest string) {
var value float64
var err = db.QueryRow(mysqlRequest).Scan(&value)
if err != nil {
log.Fatal(err)
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, value)
}
func mtQueryGaugeVec(db *sql.DB, ch chan<- prometheus.Metric, desc *prometheus.GaugeVec, mysqlRequest string) {
rows, err := db.Query(mysqlRequest)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
desc.Reset()
for rows.Next() {
var label string
var value float64
err = rows.Scan(&label, &value)
if err != nil {
log.Fatal(err)
}
desc.WithLabelValues(label).Set(value)
}
desc.Collect(ch)
}
func main() {
mtHostPtr := flag.String("host", "127.0.0.1", "Hostname or Address for DB server")
mtPortPtr := flag.String("port", "3306", "DB server port")
mtNamePtr := flag.String("db", "", "DB name")
mtUserPtr := flag.String("user", "", "DB user for connection")
mtPassPtr := flag.String("pass", "", "DB password for connection")
mtTablePrefixPtr := flag.String("tableprefix", "", "Table prefix for Mautic tables")
flag.Parse()
dbHost := fmt.Sprintf("%s:%s", *mtHostPtr, *mtPortPtr)
dbName := *mtNamePtr
dbUser := *mtUserPtr
dbPassword := *mtPassPtr
tablePrefix := *mtTablePrefixPtr
if dbName == "" {
fmt.Fprintf(os.Stderr, "flag -db=dbname required!\n")
os.Exit(1)
}
if dbUser == "" {
fmt.Fprintf(os.Stderr, "flag -user=username required!\n")
os.Exit(1)
}
//We create the collector
collector := newMauticCollector(dbHost, dbName, dbUser, dbPassword, tablePrefix)
prometheus.MustRegister(collector)
//This section will start the HTTP server and expose
//any metrics on the /metrics endpoint.
http.Handle("/metrics", promhttp.Handler())
log.Info("Beginning to serve on port :9851")
log.Fatal(http.ListenAndServe(":9851", nil))
}