Class: Triglav::Agent::Vertica::Monitor

Inherits:
Base::Monitor
  • Object
show all
Defined in:
lib/triglav/agent/vertica/monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, resource_uri_prefix, resource) ⇒ Monitor

resource:

uri: vertica://host/database/schema/table
unit: 'daily', 'hourly', 'singular', or their combinations such as 'singular,daily,hourly'
timezone: '+09:00'
span_in_days: 32

View is not supported

Parameters:



23
24
25
26
27
28
29
30
# File 'lib/triglav/agent/vertica/monitor.rb', line 23

def initialize(connection, resource_uri_prefix, resource)
  @connection = connection
  @resource_uri_prefix = resource_uri_prefix
  @resource = resource
  @status = Triglav::Agent::Status.new(resource_uri_prefix, resource.uri)
  @periodic_last_epoch = $setting.debug? ? 0 : get_from_status_file(:periodic_last_epoch)
  @singular_last_epoch = $setting.debug? ? 0 : get_from_status_file(:singular_last_epoch)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/triglav/agent/vertica/monitor.rb', line 11

def connection
  @connection
end

#periodic_last_epochObject (readonly)

Returns the value of attribute periodic_last_epoch.



11
12
13
# File 'lib/triglav/agent/vertica/monitor.rb', line 11

def periodic_last_epoch
  @periodic_last_epoch
end

#resourceObject (readonly)

Returns the value of attribute resource.



11
12
13
# File 'lib/triglav/agent/vertica/monitor.rb', line 11

def resource
  @resource
end

#resource_uri_prefixObject (readonly)

Returns the value of attribute resource_uri_prefix.



11
12
13
# File 'lib/triglav/agent/vertica/monitor.rb', line 11

def resource_uri_prefix
  @resource_uri_prefix
end

#singular_last_epochObject (readonly)

Returns the value of attribute singular_last_epoch.



11
12
13
# File 'lib/triglav/agent/vertica/monitor.rb', line 11

def singular_last_epoch
  @singular_last_epoch
end

Instance Method Details

#get_daily_eventsObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/triglav/agent/vertica/monitor.rb', line 105

def get_daily_events
  sql = "select " \
    "#{q_date} AS d, 0 AS h, max(epoch) " \
    "from #{q_db}.#{q_schema}.#{q_table} " \
    "where #{q_date} IN ('#{dates.join("','")}') " \
    "#{q_where.empty? ? '' : "AND #{q_where} "}" \
    "group by d having max(epoch) > #{q_periodic_last_epoch} " \
    "order by d"
  query_and_get_events(:daily, sql)
end

#get_hourly_eventsObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/triglav/agent/vertica/monitor.rb', line 94

def get_hourly_events
  sql = "select " \
    "#{q_date} AS d, DATE_PART('hour', #{q_timestamp}) AS h, max(epoch) " \
    "from #{q_db}.#{q_schema}.#{q_table} " \
    "where #{q_date} IN ('#{dates.join("','")}') " \
    "#{q_where.empty? ? '' : "AND #{q_where} "}" \
    "group by d, h having max(epoch) > #{q_periodic_last_epoch} " \
    "order by d, h"
  query_and_get_events(:hourly, sql)
end

#get_periodic_eventsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/triglav/agent/vertica/monitor.rb', line 70

def get_periodic_events
  if hourly?
    events, new_last_epoch, rows = get_hourly_events
    if daily?
      daily_events = build_daily_events_from_hourly(rows)
      events.concat(daily_events)
    end
    [events, new_last_epoch]
  elsif daily?
    get_daily_events
  else
    raise
  end
end

#get_singular_eventsObject



85
86
87
88
89
90
91
92
# File 'lib/triglav/agent/vertica/monitor.rb', line 85

def get_singular_events
  sql = "select " \
    "NULL AS d, NULL AS h, max(epoch) " \
    "from #{q_db}.#{q_schema}.#{q_table} " \
    "#{q_where.empty? ? '' : "where #{q_where} "}" \
    "having max(epoch) > #{q_singular_last_epoch}"
  query_and_get_events(:singular, sql)
end

#process {|events| ... } ⇒ Object

Yields:

  • (events)


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
# File 'lib/triglav/agent/vertica/monitor.rb', line 32

def process
  unless resource_valid?
    $logger.warn { "Broken resource: #{resource.to_s}" }
    return nil
  end

  $logger.debug {
    msgs = ["Start process #{resource.uri}"]
    msgs << "periodic_last_epoch:#{periodic_last_epoch}" if periodic_last_epoch
    msgs << "singular_last_epoch:#{singular_last_epoch}" if singular_last_epoch
    msgs.join(', ')
  }

  if periodic?
    periodic_events, new_periodic_last_epoch = get_periodic_events
    events = periodic_events || []
  end
  if singular?
    singular_events, new_singular_last_epoch = get_singular_events
    events.nil? ? (events = singular_events) : events.concat(singular_events || [])
  end

  $logger.debug {
    msgs = ["Finish process #{resource.uri}"]
    msgs << "periodic_last_epoch:#{periodic_last_epoch}" if periodic_last_epoch
    msgs << "singular_last_epoch:#{singular_last_epoch}" if singular_last_epoch
    msgs << "new_periodic_last_epoch:#{new_periodic_last_epoch}" if new_periodic_last_epoch
    msgs << "new_singular_last_epoch:#{new_singular_last_epoch}" if new_singular_last_epoch
    msgs.join(', ')
  }

  return nil if events.nil? || events.empty?
  yield(events) if block_given? # send_message
  update_status_file(:periodic_last_epoch, new_periodic_last_epoch) if new_periodic_last_epoch
  update_status_file(:singular_last_epoch, new_singular_last_epoch) if new_singular_last_epoch
  true
end