-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwordpress_exporter.go
282 lines (233 loc) · 10.6 KB
/
wordpress_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
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"
"strconv"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
const (
namespace = "wordpress"
)
//This is my collector metrics
type wpCollector struct {
numPostsMetric *prometheus.GaugeVec
numCommentsMetric *prometheus.GaugeVec
numUsersMetric *prometheus.Desc
numCustomersMetric *prometheus.Desc
numSessionsMetric *prometheus.Desc
numWebhooksMetric *prometheus.GaugeVec
numAutoloadMetric *prometheus.Desc
numAutoloadSizeMetric *prometheus.Desc
numDatabaseSizeMetric *prometheus.Desc
numPostsTypeMetric *prometheus.GaugeVec
numOrderTypeMetric *prometheus.GaugeVec
dbHost string
dbName string
dbUser string
dbPass string
dbTablePrefix string
dbSkipWooCommerce bool
}
//This is a constructor for my wpCollector struct
func newWordPressCollector(host string, dbname string, username string, pass string, tablePrefix string, skipWooCommerce bool) *wpCollector {
return &wpCollector{
numUsersMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "users_total"),
"Shows the number of registered users in the WordPress site",
nil, nil,
),
numCustomersMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "customers_total"),
"Shows the number of customers in the WordPress site",
nil, nil,
),
numCommentsMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "comments_total",
Help: "Shows the number of total comments in the WordPress site",
},
[]string{"type"},
),
numSessionsMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "user_sessions_total"),
"Shows the number of sessions in the WordPress site",
nil, nil,
),
numPostsMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "posts_total",
Help: "Shows the number of total posts in the WordPress site",
},
[]string{"type"},
),
numWebhooksMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "webhooks_total",
Help: "Shows the number of webhooks in the WordPress site",
},
[]string{"status"},
),
numAutoloadMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "option_autoload_total"),
"Shows the number of options with autoload",
nil, nil,
),
numAutoloadSizeMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "option_autoload_bytes"),
"Shows the size in bytes of options with autoload",
nil, nil,
),
numDatabaseSizeMetric: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "database_size_bytes"),
"Shows the size in bytes of the wordpress's database",
nil, nil,
),
numPostsTypeMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "posts_type_total",
Help: "Shows the number of total posts type in the WordPress site",
},
[]string{"type"},
),
numOrderTypeMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "order_type_total",
Help: "Shows the number of total orders type in WooCommerce",
},
[]string{"type"},
),
dbHost: host,
dbName: dbname,
dbUser: username,
dbPass: pass,
dbTablePrefix: tablePrefix,
dbSkipWooCommerce: skipWooCommerce,
}
}
//Describe method is required for a prometheus.Collector type
func (collector *wpCollector) Describe(ch chan<- *prometheus.Desc) {
//We set the metrics
ch <- collector.numUsersMetric
ch <- collector.numCustomersMetric
ch <- collector.numSessionsMetric
ch <- collector.numAutoloadMetric
ch <- collector.numAutoloadSizeMetric
ch <- collector.numDatabaseSizeMetric
collector.numOrderTypeMetric.Describe(ch)
collector.numPostsTypeMetric.Describe(ch)
collector.numCommentsMetric.Describe(ch)
collector.numPostsMetric.Describe(ch)
collector.numWebhooksMetric.Describe(ch)
}
//Collect method is required for a prometheus.Collector type
func (collector *wpCollector) 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 count(*) as value from wp_users;
queryNumUsersMetric := fmt.Sprintf("select count(*) as value from %susers;", collector.dbTablePrefix)
wpQueryGauge(db, ch, collector.numUsersMetric, queryNumUsersMetric)
//select count(*) as value from wp_users inner join wp_usermeta on wp_users.ID = wp_usermeta.user_id where wp_usermeta.meta_key = 'wp_capabilities' and wp_usermeta.meta_value like '%customer%';
queryNumCustomersMetric := fmt.Sprintf("select count(*) as value from %susers inner join %susermeta on %susers.ID = %susermeta.user_id where %susermeta.meta_key = 'wp_capabilities' and %susermeta.meta_value like '%%customer%%';", collector.dbTablePrefix, collector.dbTablePrefix, collector.dbTablePrefix, collector.dbTablePrefix, collector.dbTablePrefix, collector.dbTablePrefix)
wpQueryGauge(db, ch, collector.numCustomersMetric, queryNumCustomersMetric)
//select COALESCE(NULLIF(comment_type, ''), 'comment') as label, count(*) as value from wp_comments group by comment_type;
queryNumCommentsMetric := fmt.Sprintf("select COALESCE(NULLIF(comment_type, ''), 'comment') as label, count(*) as value from %scomments group by comment_type;", collector.dbTablePrefix)
wpQueryGaugeVec(db, ch, collector.numCommentsMetric, queryNumCommentsMetric)
//select post_status as label, count(*) as value from wp_posts WHERE post_type='post' GROUP BY post_status;
queryNumPostsMetric := fmt.Sprintf("select post_status as label, count(*) as value from %sposts WHERE post_type='post' GROUP BY post_status;", collector.dbTablePrefix)
wpQueryGaugeVec(db, ch, collector.numPostsMetric, queryNumPostsMetric)
if !collector.dbSkipWooCommerce {
//select count(*) as numSessions from wp_woocommerce_sessions;
queryNumSessionsMetric := fmt.Sprintf("select count(*) as numSessions from %swoocommerce_sessions;", collector.dbTablePrefix)
wpQueryGauge(db, ch, collector.numSessionsMetric, queryNumSessionsMetric)
}
//select post_status as label, count(*) as value from wp_posts WHERE post_type='scheduled-action' GROUP BY post_status;
queryNumWebhooksMetric := fmt.Sprintf("select post_status as label, count(*) as value from %sposts WHERE post_type='scheduled-action' GROUP BY post_status;", collector.dbTablePrefix)
wpQueryGaugeVec(db, ch, collector.numWebhooksMetric, queryNumWebhooksMetric)
//select count(*) from wp_options where autoload = 'yes';
queryNumAutoloadMetric := fmt.Sprintf("select count(*) from %soptions where autoload = 'yes';", collector.dbTablePrefix)
wpQueryGauge(db, ch, collector.numAutoloadMetric, queryNumAutoloadMetric)
//select ROUND(SUM(LENGTH(option_value))) as value from wp_options where autoload = 'yes';
queryNumAutoloadSizeMetric := fmt.Sprintf("select ROUND(SUM(LENGTH(option_value))) as value from %soptions where autoload = 'yes';", collector.dbTablePrefix)
wpQueryGauge(db, ch, collector.numAutoloadSizeMetric, queryNumAutoloadSizeMetric)
//select count(*) from wp_options where autoload = 'yes';
queryNumDatabaseSizeMetric := fmt.Sprintf("select ROUND(SUM(data_length+index_length),2) as value from information_schema.tables where table_schema='%s';", collector.dbName)
wpQueryGauge(db, ch, collector.numDatabaseSizeMetric, queryNumDatabaseSizeMetric)
//select post_type, count(*) from wp_posts group by post_type;
queryNumPostsTypeMetric := fmt.Sprintf("select post_type, count(*) from %sposts group by post_type;", collector.dbTablePrefix)
wpQueryGaugeVec(db, ch, collector.numPostsTypeMetric, queryNumPostsTypeMetric)
//select SUBSTRING(post_status, 4), count(*) from wp_posts where post_type = 'shop_order' group by post_status;
queryNumOrderTypeMetric := fmt.Sprintf("select SUBSTRING(post_status, 4), count(*) from %sposts where post_type = 'shop_order' group by post_status;", collector.dbTablePrefix)
wpQueryGaugeVec(db, ch, collector.numOrderTypeMetric, queryNumOrderTypeMetric)
}
func wpQueryCounter(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 wpQueryGauge(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 wpQueryGaugeVec(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() {
wpHostPtr := flag.String("host", "127.0.0.1", "Hostname or Address for DB server")
wpPortPtr := flag.String("port", "3306", "DB server port")
wpNamePtr := flag.String("db", "", "DB name")
wpUserPtr := flag.String("user", "", "DB user for connection")
wpPassPtr := flag.String("pass", "", "DB password for connection")
wpTablePrefixPtr := flag.String("tableprefix", "wp_", "Table prefix for WordPress tables")
wpSkipWooCommerce := flag.String("skipwoocommerce", "false", "Skip WooCommerce metrics")
flag.Parse()
dbHost := fmt.Sprintf("%s:%s", *wpHostPtr, *wpPortPtr)
dbName := *wpNamePtr
dbUser := *wpUserPtr
dbPassword := *wpPassPtr
tablePrefix := *wpTablePrefixPtr
skipWooCommerce, _ := strconv.ParseBool(*wpSkipWooCommerce)
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 := newWordPressCollector(dbHost, dbName, dbUser, dbPassword, tablePrefix, skipWooCommerce)
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 :9850")
log.Fatal(http.ListenAndServe(":9850", nil))
}