| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/prometheus/client_golang/prometheus" |
| 7 | "github.com/sirupsen/logrus" |
| 8 | "github.com/xperimental/nextcloud-exporter/internal/client" |
| 9 | "github.com/xperimental/nextcloud-exporter/serverinfo" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | metricPrefix = "nextcloud_" |
| 14 | |
| 15 | labelErrorCauseOther = "other" |
| 16 | labelErrorCauseAuth = "auth" |
| 17 | labelErrorCauseRatelimit = "ratelimit" |
| 18 | ) |
| 19 | |
| 20 | var ( |
| 21 | systemInfoDesc = prometheus.NewDesc( |
| 22 | metricPrefix+"system_info", |
| 23 | "Contains meta information about Nextcloud as labels. Value is always 1.", |
| 24 | []string{"version"}, nil) |
| 25 | systemUpdateAvailableDesc = prometheus.NewDesc( |
| 26 | metricPrefix+"system_update_available", |
| 27 | "Contains information whether a system update is available (0 = no, 1 = yes). The available_version label contains the latest available nextcloud version, whereas the version label contains the current installed nextcloud version.", |
| 28 | []string{"version", "available_version"}, nil) |
| 29 | appsInstalledDesc = prometheus.NewDesc( |
| 30 | metricPrefix+"apps_installed_total", |
| 31 | "Number of currently installed apps", |
| 32 | nil, nil) |
| 33 | appsUpdatesDesc = prometheus.NewDesc( |
| 34 | metricPrefix+"apps_updates_available_total", |
| 35 | "Number of apps that have available updates", |
| 36 | nil, nil) |
| 37 | usersDesc = prometheus.NewDesc( |
| 38 | metricPrefix+"users_total", |
| 39 | "Number of users of the instance.", |
| 40 | nil, nil) |
| 41 | filesDesc = prometheus.NewDesc( |
| 42 | metricPrefix+"files_total", |
| 43 | "Number of files served by the instance.", |
| 44 | nil, nil) |
| 45 | freeSpaceDesc = prometheus.NewDesc( |
| 46 | metricPrefix+"free_space_bytes", |
| 47 | "Free disk space in data directory in bytes.", |
| 48 | nil, nil) |
| 49 | sharesDesc = prometheus.NewDesc( |
| 50 | metricPrefix+"shares_total", |
| 51 | "Number of shares by type.", |
| 52 | []string{"type"}, nil) |
| 53 | federationsDesc = prometheus.NewDesc( |
| 54 | metricPrefix+"shares_federated_total", |
| 55 | "Number of federated shares by direction.", |
| 56 | []string{"direction"}, nil) |
| 57 | activeUsersDesc = prometheus.NewDesc( |
| 58 | metricPrefix+"active_users_total", |
| 59 | "Number of active users for the last five minutes.", |
| 60 | nil, nil) |
| 61 | hourlyActiveUsersDesc = prometheus.NewDesc( |
| 62 | metricPrefix+"active_users_hourly_total", |
| 63 | "Number of active users in the last hour.", |
| 64 | nil, nil) |
| 65 | dailyActiveUsersDesc = prometheus.NewDesc( |
| 66 | metricPrefix+"active_users_daily_total", |
| 67 | "Number of active users in the last 24 hours.", |
| 68 | nil, nil) |
| 69 | phpInfoDesc = prometheus.NewDesc( |
| 70 | metricPrefix+"php_info", |
| 71 | "Contains meta information about PHP as labels. Value is always 1.", |
| 72 | []string{"version"}, nil) |
| 73 | phpMemoryLimitDesc = prometheus.NewDesc( |
| 74 | metricPrefix+"php_memory_limit_bytes", |
| 75 | "Configured PHP memory limit in bytes.", |
| 76 | nil, nil) |
| 77 | phpMaxUploadSizeDesc = prometheus.NewDesc( |
| 78 | metricPrefix+"php_upload_max_size_bytes", |
| 79 | "Configured maximum upload size in bytes.", |
| 80 | nil, nil) |
| 81 | databaseInfoDesc = prometheus.NewDesc( |
| 82 | metricPrefix+"database_info", |
| 83 | "Contains meta information about the database as labels. Value is always 1.", |
| 84 | []string{"version", "type"}, nil) |
| 85 | databaseSizeDesc = prometheus.NewDesc( |
| 86 | metricPrefix+"database_size_bytes", |
| 87 | "Size of database in bytes as reported from engine.", |
| 88 | nil, nil) |
| 89 | ) |
| 90 | |
| 91 | type nextcloudCollector struct { |
| 92 | log logrus.FieldLogger |
| 93 | infoClient client.InfoClient |
| 94 | appsMetrics bool |
| 95 | updateMetrics bool |
| 96 | |
| 97 | upMetric prometheus.Gauge |
| 98 | scrapeErrorsMetric *prometheus.CounterVec |
| 99 | } |
| 100 | |
| 101 | func RegisterCollector(log logrus.FieldLogger, infoClient client.InfoClient, appsMetrics bool, updateMetrics bool) error { |
| 102 | c := &nextcloudCollector{ |
| 103 | log: log, |
| 104 | infoClient: infoClient, |
| 105 | appsMetrics: appsMetrics, |
| 106 | updateMetrics: updateMetrics, |
| 107 | |
| 108 | upMetric: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 109 | Name: metricPrefix + "up", |
| 110 | Help: "Indicates if the metrics could be scraped by the exporter.", |
| 111 | }), |
| 112 | scrapeErrorsMetric: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 113 | Name: metricPrefix + "scrape_errors_total", |
| 114 | Help: "Counts the number of scrape errors by this collector.", |
| 115 | }, []string{"cause"}), |
| 116 | } |
| 117 | |
| 118 | return prometheus.Register(c) |
| 119 | } |
| 120 | |
| 121 | func (c *nextcloudCollector) Describe(ch chan<- *prometheus.Desc) { |
| 122 | c.upMetric.Describe(ch) |
| 123 | c.scrapeErrorsMetric.Describe(ch) |
| 124 | ch <- usersDesc |
| 125 | ch <- filesDesc |
| 126 | ch <- freeSpaceDesc |
| 127 | ch <- sharesDesc |
| 128 | ch <- federationsDesc |
| 129 | ch <- activeUsersDesc |
| 130 | ch <- hourlyActiveUsersDesc |
| 131 | ch <- dailyActiveUsersDesc |
| 132 | } |
| 133 | |
| 134 | func (c *nextcloudCollector) Collect(ch chan<- prometheus.Metric) { |
| 135 | if err := c.collectNextcloud(ch); err != nil { |
| 136 | c.log.Errorf("Error during scrape: %s", err) |
| 137 | |
| 138 | cause := labelErrorCauseOther |
| 139 | if err == client.ErrNotAuthorized { |
| 140 | cause = labelErrorCauseAuth |
| 141 | } else if err == client.ErrRatelimit { |
| 142 | cause = labelErrorCauseRatelimit |
| 143 | } |
| 144 | c.scrapeErrorsMetric.WithLabelValues(cause).Inc() |
| 145 | c.upMetric.Set(0) |
| 146 | } else { |
| 147 | c.upMetric.Set(1) |
| 148 | } |
| 149 | |
| 150 | c.upMetric.Collect(ch) |
| 151 | c.scrapeErrorsMetric.Collect(ch) |
| 152 | } |
| 153 | |
| 154 | func (c *nextcloudCollector) collectNextcloud(ch chan<- prometheus.Metric) error { |
| 155 | status, err := c.infoClient() |
| 156 | if err != nil { |
| 157 | return err |
| 158 | } |
| 159 | |
| 160 | return readMetrics(ch, status, c.appsMetrics, c.updateMetrics) |
| 161 | } |
| 162 | |
| 163 | func readMetrics(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo, appsMetrics bool, updateMetrics bool) error { |
| 164 | if err := collectSimpleMetrics(ch, status, appsMetrics); err != nil { |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | if updateMetrics { |
| 169 | if err := collectUpdate(ch, status); err != nil { |
| 170 | return err |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if err := collectShares(ch, status.Data.Nextcloud.Shares); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | if err := collectFederatedShares(ch, status.Data.Nextcloud.Shares); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | |
| 182 | systemInfo := []string{ |
| 183 | status.Data.Nextcloud.System.Version, |
| 184 | } |
| 185 | if err := collectInfoMetric(ch, systemInfoDesc, systemInfo); err != nil { |
| 186 | return err |
| 187 | } |
| 188 | |
| 189 | phpInfo := []string{ |
| 190 | status.Data.Server.PHP.Version, |
| 191 | } |
| 192 | if err := collectInfoMetric(ch, phpInfoDesc, phpInfo); err != nil { |
| 193 | return err |
| 194 | } |
| 195 | |
| 196 | databaseInfo := []string{ |
| 197 | status.Data.Server.Database.Version, |
| 198 | status.Data.Server.Database.Type, |
| 199 | } |
| 200 | if err := collectInfoMetric(ch, databaseInfoDesc, databaseInfo); err != nil { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | type simpleMetric struct { |
| 208 | desc *prometheus.Desc |
| 209 | value float64 |
| 210 | } |
| 211 | |
| 212 | func collectSimpleMetrics(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo, appsMetrics bool) error { |
| 213 | metrics := []simpleMetric{ |
| 214 | { |
| 215 | desc: usersDesc, |
| 216 | value: float64(status.Data.Nextcloud.Storage.Users), |
| 217 | }, |
| 218 | { |
| 219 | desc: filesDesc, |
| 220 | value: float64(status.Data.Nextcloud.Storage.Files), |
| 221 | }, |
| 222 | { |
| 223 | desc: freeSpaceDesc, |
| 224 | value: status.Data.Nextcloud.System.FreeSpace, |
| 225 | }, |
| 226 | { |
| 227 | desc: activeUsersDesc, |
| 228 | value: float64(status.Data.ActiveUsers.Last5Minutes), |
| 229 | }, |
| 230 | { |
| 231 | desc: hourlyActiveUsersDesc, |
| 232 | value: float64(status.Data.ActiveUsers.LastHour), |
| 233 | }, |
| 234 | { |
| 235 | desc: dailyActiveUsersDesc, |
| 236 | value: float64(status.Data.ActiveUsers.LastDay), |
| 237 | }, |
| 238 | { |
| 239 | desc: phpMemoryLimitDesc, |
| 240 | value: float64(status.Data.Server.PHP.MemoryLimit), |
| 241 | }, |
| 242 | { |
| 243 | desc: phpMaxUploadSizeDesc, |
| 244 | value: float64(status.Data.Server.PHP.UploadMaxFilesize), |
| 245 | }, |
| 246 | { |
| 247 | desc: databaseSizeDesc, |
| 248 | value: float64(status.Data.Server.Database.Size), |
| 249 | }, |
| 250 | } |
| 251 | |
| 252 | if appsMetrics { |
| 253 | metrics = append(metrics, []simpleMetric{ |
| 254 | { |
| 255 | desc: appsInstalledDesc, |
| 256 | value: float64(status.Data.Nextcloud.System.Apps.Installed), |
| 257 | }, |
| 258 | { |
| 259 | desc: appsUpdatesDesc, |
| 260 | value: float64(status.Data.Nextcloud.System.Apps.AvailableUpdates), |
| 261 | }, |
| 262 | }...) |
| 263 | } |
| 264 | |
| 265 | for _, m := range metrics { |
| 266 | metric, err := prometheus.NewConstMetric(m.desc, prometheus.GaugeValue, m.value) |
| 267 | if err != nil { |
| 268 | return fmt.Errorf("error creating metric for %s: %w", m.desc, err) |
| 269 | } |
| 270 | ch <- metric |
| 271 | } |
| 272 | |
| 273 | return nil |
| 274 | } |
| 275 | |
| 276 | func collectUpdate(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo) error { |
| 277 | systemInfo := status.Data.Nextcloud.System |
| 278 | updateAvailableValue := 0.0 |
| 279 | |
| 280 | // Fix small bug: its indicated as "true" even if there is no real update available. |
| 281 | if systemInfo.Update.Available && systemInfo.Version != systemInfo.Update.AvailableVersion { |
| 282 | updateAvailableValue = 1.0 |
| 283 | } |
| 284 | |
| 285 | metric, err := prometheus.NewConstMetric(systemUpdateAvailableDesc, prometheus.GaugeValue, updateAvailableValue, systemInfo.Version, systemInfo.Update.AvailableVersion) |
| 286 | if err != nil { |
| 287 | return fmt.Errorf("error creating metric for %s: %w", systemUpdateAvailableDesc, err) |
| 288 | } |
| 289 | ch <- metric |
| 290 | |
| 291 | return nil |
| 292 | } |
| 293 | |
| 294 | func collectShares(ch chan<- prometheus.Metric, shares serverinfo.Shares) error { |
| 295 | values := make(map[string]float64) |
| 296 | values["user"] = float64(shares.SharesUser) |
| 297 | values["group"] = float64(shares.SharesGroups) |
| 298 | values["authlink"] = float64(shares.SharesLink - shares.SharesLinkNoPassword) |
| 299 | values["link"] = float64(shares.SharesLink) |
| 300 | values["mail"] = float64(shares.SharesMail) |
| 301 | values["room"] = float64(shares.SharesRoom) |
| 302 | |
| 303 | return collectMap(ch, sharesDesc, values) |
| 304 | } |
| 305 | |
| 306 | func collectFederatedShares(ch chan<- prometheus.Metric, shares serverinfo.Shares) error { |
| 307 | values := make(map[string]float64) |
| 308 | values["sent"] = float64(shares.FedSent) |
| 309 | values["received"] = float64(shares.FedReceived) |
| 310 | |
| 311 | return collectMap(ch, federationsDesc, values) |
| 312 | } |
| 313 | |
| 314 | func collectMap(ch chan<- prometheus.Metric, desc *prometheus.Desc, labelValueMap map[string]float64) error { |
| 315 | for k, v := range labelValueMap { |
| 316 | metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, v, k) |
| 317 | if err != nil { |
| 318 | return fmt.Errorf("error creating shares metric for %s: %w", k, err) |
| 319 | } |
| 320 | ch <- metric |
| 321 | } |
| 322 | |
| 323 | return nil |
| 324 | } |
| 325 | |
| 326 | func collectInfoMetric(ch chan<- prometheus.Metric, desc *prometheus.Desc, labelValues []string) error { |
| 327 | metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, 1, labelValues...) |
| 328 | if err != nil { |
| 329 | return err |
| 330 | } |
| 331 | |
| 332 | ch <- metric |
| 333 | return nil |
| 334 | } |