Module: ElasticsearchQuery
- Included in:
- ElasticsearchCommon
- Defined in:
- lib/sensu-plugins-elasticsearch/elasticsearch-query.rb
Overview
DESCRIPTION:
Common search helper methods
DEPENDENCIES:
gem: elasticsearch
gem: sensu-plugin
USAGE:
NOTES:
LICENSE:
Brendan Gibat <[email protected]>
Released under the same terms as Sensu (the MIT license); see LICENSE
for details.
Instance Method Summary collapse
- #build_request_options ⇒ Object
- #es_date_math_string(end_time) ⇒ Object
- #indices(end_time) ⇒ Object
- #initialize ⇒ Object
Instance Method Details
#build_request_options ⇒ Object
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 |
# File 'lib/sensu-plugins-elasticsearch/elasticsearch-query.rb', line 63 def end_time = (Time.now.utc - config[:offset]) = { index: indices(end_time), ignore_unavailable: true } unless config[:ignore_unavailable].nil? [:ignore_unavailable] = config[:ignore_unavailable] end unless config[:id].nil? [:id] = config[:id] end if !config[:body].nil? [:body] = config[:body] elsif config[:aggr] == true es_date_start = es_date_math_string end_time [:size] = 0 [:body] = { 'query' => { 'bool' => { 'must' => [{ 'query_string' => { 'default_field' => config[:search_field], 'query' => config[:query] } }, { 'range' => { config[:timestamp_field] => { 'gt' => es_date_start, 'lt' => end_time.strftime('%Y-%m-%dT%H:%M:%S') } } }] } }, 'aggregations' => { 'average' => { 'avg' => { 'field' => config[:aggr_field] } } } } else es_date_start = es_date_math_string end_time unless es_date_start.nil? [:body] = { 'query' => { 'bool' => { 'must' => [{ 'query_string' => { 'default_field' => config[:search_field], 'query' => config[:query] } }, { 'range' => { config[:timestamp_field] => { 'gt' => es_date_start, 'lt' => end_time.strftime('%Y-%m-%dT%H:%M:%S') } } }] } } } end end unless config[:types].nil? [:type] = config[:types] end end |
#es_date_math_string(end_time) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/sensu-plugins-elasticsearch/elasticsearch-query.rb', line 136 def es_date_math_string(end_time) if config[:minutes_previous].zero? && \ config[:hours_previous].zero? && \ config[:days_previous].zero? && \ config[:weeks_previous].zero? && \ config[:months_previous].zero? nil else es_math = "#{end_time.strftime '%Y-%m-%dT%H:%M:%S'}||" es_math += "-#{config[:minutes_previous]}m" if config[:minutes_previous] != 0 es_math += "-#{config[:hours_previous]}h" if config[:hours_previous] != 0 es_math += "-#{config[:days_previous]}d" if config[:days_previous] != 0 es_math += "-#{config[:weeks_previous]}w" if config[:weeks_previous] != 0 es_math += "-#{config[:months_previous]}M" if config[:months_previous] != 0 es_math end end |
#indices(end_time) ⇒ Object
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 |
# File 'lib/sensu-plugins-elasticsearch/elasticsearch-query.rb', line 24 def indices(end_time) if !config[:index].nil? return config[:index] elsif !config[:date_index].nil? indices = [] curr = end_time.to_i start = curr if config[:minutes_previous] != 0 start -= (config[:minutes_previous] * 60) end if config[:hours_previous] != 0 start -= (config[:hours_previous] * 60 * 60) end if config[:days_previous] != 0 start -= (config[:days_previous] * 60 * 60 * 24) end if config[:weeks_previous] != 0 start -= (config[:weeks_previous] * 60 * 60 * 24 * 7) end if config[:months_previous] != 0 start -= (config[:months_previous] * 60 * 60 * 24 * 7 * 31) end total = 60 * 60 * 24 if config[:date_repeat_hourly] total = 60 * 60 end (start.to_i..curr.to_i).step(total) do |step| indices.push(Time.at(step).utc.strftime config[:date_index]) end unless indices.include?(Time.at(curr).utc.strftime config[:date_index]) indices.push(Time.at(curr).utc.strftime config[:date_index]) end return indices.join(',') end ['_all'] end |
#initialize ⇒ Object
20 21 22 |
# File 'lib/sensu-plugins-elasticsearch/elasticsearch-query.rb', line 20 def initialize super() end |