Class: Sensu::Plugins::Prometheus::Metrics
- Inherits:
-
Object
- Object
- Sensu::Plugins::Prometheus::Metrics
- Includes:
- Utils::Log
- Defined in:
- lib/sensu/plugins/prometheus/metrics.rb
Overview
Represents the queryies that will be fired against Prometheus to collect information about monitored resources. Ideally all public methods on this class will be available as a final check.
Instance Method Summary collapse
-
#custom(cfg) ⇒ Object
Execute query informed on check’s configuration and makes no modifications on value.
-
#disk(cfg) ⇒ Object
Query percentage of mountpoint total disk space size compared with avaiable.
-
#disk_all(cfg) ⇒ Object
Query percentage of free space on file-systems, ignoring by default ‘tmpfs` or the regexp configured on check.
-
#initialize(prometheus_client) ⇒ Metrics
constructor
A new instance of Metrics.
-
#inode(cfg) ⇒ Object
Queyr percentage of free inodes on check’s configured mountpoint.
-
#load_per_cluster(cfg) ⇒ Object
Calculates the load of an entire cluster.
-
#load_per_cluster_minus_n(cfg) ⇒ Object
Returns a single metric entry, with the sum of the total load on cluster divided by the total amount of CPUs.
-
#load_per_cpu(_) ⇒ Object
Current load per CPU.
-
#memory(_) ⇒ Object
Query the percentage free memory.
-
#memory_per_cluster(cfg) ⇒ Object
Percentage free memory cluster wide.
-
#predict_disk_all(cfg) ⇒ Object
Compose query to predict disk usage on the last day.
-
#service(cfg) ⇒ Object
Service metrics will contain it’s “state” as “value”.
Methods included from Utils::Log
Constructor Details
#initialize(prometheus_client) ⇒ Metrics
Returns a new instance of Metrics.
12 13 14 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 12 def initialize(prometheus_client) @client = prometheus_client end |
Instance Method Details
#custom(cfg) ⇒ Object
Execute query informed on check’s configuration and makes no modifications on value.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 18 def custom(cfg) metrics = [] @client.query(cfg['query']).each do |result| source = if result['metric']['instance'] =~ /^\d+/ result['metric']['app'] else result['metric']['instance'] end metrics << { 'source' => source, 'value' => result['value'][1] } end metrics end |
#disk(cfg) ⇒ Object
Query percentage of mountpoint total disk space size compared with avaiable.
37 38 39 40 41 42 43 44 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 37 def disk(cfg) mountpoint = "mountpoint=\"#{cfg['mount']}\"" query = @client.percent_query_free( "node_filesystem_size{#{mountpoint}}", "node_filesystem_avail{#{mountpoint}}" ) prepare_metrics('disk', @client.query(query)) end |
#disk_all(cfg) ⇒ Object
Query percentage of free space on file-systems, ignoring by default ‘tmpfs` or the regexp configured on check.
48 49 50 51 52 53 54 55 56 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 48 def disk_all(cfg) ignored = cfg['ignore_fs'] || 'tmpfs' ignore_fs = "fstype!~\"#{ignored}\"" query = @client.percent_query_free( "node_filesystem_files{#{ignore_fs}}", "node_filesystem_files_free{#{ignore_fs}}" ) prepare_metrics('disk_all', @client.query(query)) end |
#inode(cfg) ⇒ Object
Queyr percentage of free inodes on check’s configured mountpoint.
59 60 61 62 63 64 65 66 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 59 def inode(cfg) mountpoint = "mountpoint=\"#{cfg['mount']}\"" query = @client.percent_query_free( "node_filesystem_files{#{mountpoint}}", "node_filesystem_files_free{#{mountpoint}}" ) prepare_metrics('inode', @client.query(query)) end |
#load_per_cluster(cfg) ⇒ Object
Calculates the load of an entire cluster.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 122 def load_per_cluster(cfg) cluster = cfg['cluster'] query = format( 'sum(node_load5{job="%s"})/count(node_cpu{mode="system",job="%s"})', cluster, cluster ) result = @client.query(query).first source = cfg['source'] value = result['value'][1].to_f.round(2) log.debug( "[load_per_cluster] value: '#{value}', source: '#{source}'" ) [{ 'source' => source, 'value' => value }] end |
#load_per_cluster_minus_n(cfg) ⇒ Object
Returns a single metric entry, with the sum of the total load on cluster divided by the total amount of CPUs.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 142 def load_per_cluster_minus_n(cfg) cluster = cfg['cluster'] minus_n = cfg['minus_n'] sum_load = "sum(node_load5{job=\"#{cluster}\"})" total_cpus = "count(node_cpu{mode=\"system\",job=\"#{cluster}\"})" total_nodes = "count(node_load5{job=\"#{cluster}\"})" query = format( '%s/(%s-(%s/%s)*%d)', sum_load, total_cpus, total_cpus, total_nodes, minus_n ) result = @client.query(query).first value = result['value'][1].to_f.round(2) source = cfg['source'] log.debug( "[load_per_cluster_minus_n] value: '#{value}', source: '#{source}'" ) [{ 'source' => source, 'value' => value }] end |
#load_per_cpu(_) ⇒ Object
Current load per CPU.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 164 def load_per_cpu(_) cpu_per_source = {} @client.query( '(count(node_cpu{mode="system"})by(instance))' ).each do |result| source = result['metric']['instance'] cpu_per_source[source] = result['value'][1] end metrics = [] @client.query('node_load5').each do |result| source = result['metric']['instance'] value = result['value'][1].to_f.round(2) load_on_cpu = value / cpu_per_source[source].to_f log.debug( "[load_per_cpu] value: '#{load_on_cpu}', source: '#{source}'" ) metrics << { 'source' => source, 'value' => load_on_cpu } end metrics end |
#memory(_) ⇒ Object
Query the percentage free memory.
95 96 97 98 99 100 101 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 95 def memory(_) query = @client.percent_query_free( 'node_memory_MemTotal', 'node_memory_MemAvailable' ) prepare_metrics('memory', @client.query(query)) end |
#memory_per_cluster(cfg) ⇒ Object
Percentage free memory cluster wide.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 104 def memory_per_cluster(cfg) cluster = cfg['cluster'] query = @client.percent_query_free( "sum(node_memory_MemTotal{job=\"#{cluster}\"})", "sum(node_memory_MemAvailable{job=\"#{cluster}\"})" ) metrics = [] source = cfg['source'] @client.query(query).each do |result| value = result['value'][1].to_f.round(2) log.debug("[memory_per_cluster] value: '#{value}', source: '#{source}'") metrics << { 'source' => source, 'value' => value } end metrics end |
#predict_disk_all(cfg) ⇒ Object
Compose query to predict disk usage on the last day.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 69 def predict_disk_all(cfg) days = cfg['days'].to_i days_in_seconds = days.to_i * 86_400 filter = cfg['filter'] || {} range_vector = cfg['sample_size'] || '24h' query = format( 'predict_linear(node_filesystem_avail%s[%s], %i) < 0', filter, range_vector, days_in_seconds ) prepare_metrics('predict_disk_all', @client.query(query)) end |
#service(cfg) ⇒ Object
Service metrics will contain it’s “state” as “value”.
84 85 86 87 88 89 90 91 92 |
# File 'lib/sensu/plugins/prometheus/metrics.rb', line 84 def service(cfg) defaults = { 'state' => 'active' } cfg = defaults.merge(cfg) query = format( "node_systemd_unit_state{name='%s',state='%s'}", cfg['name'], cfg['state'] ) prepare_metrics('service', @client.query(query)) end |