sysdig 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 976ab5bf9c0d56e508aabe8cce280db889904138
4
- data.tar.gz: 951c22606a8f96b4d4f74d5d3590a15c038318dc
3
+ metadata.gz: 7b60fd31dd65eec996331f14d9d49ed12a8bae20
4
+ data.tar.gz: 14044e9a5d576997010f004e49e3197eeba123ad
5
5
  SHA512:
6
- metadata.gz: 41ae7f0e86f9717310d8b327531a594f5afc1ce593ee1171ab98f9f5729f8509681a44a7691bc25e3c7671478adfe9b0e63b81d101da75db398663d4e57b2393
7
- data.tar.gz: 5d15fc64b7900fba201241aa6a43bba690102e5f129b89bf37cb9b91e71419f97a588a20b0f30fe55805f2248ed77658ab8281968a2b7edbffaefe0faa862eef
6
+ metadata.gz: a7ce69fd877635ce055a49d596b8a38608241b2119949672a1e771de3e6f9aa290af9de17b36e39d15ab0b59514e2e7cab4d35089ac0662c41ea193ac02f0c75
7
+ data.tar.gz: ee96c4e902ae142ab244f72d24b34d4b3941f3184c57e7b5e358324a912961c0392e81dcb7220f528ec317d7da3dffa7ba0e809734ad284023b3bd29acf661b2
data/lib/sysdig.rb CHANGED
@@ -12,7 +12,6 @@ class Sysdig
12
12
  include Cistern::Client
13
13
 
14
14
  recognizes :url, :adapter, :logger, :token, :username, :password
15
-
16
15
  end
17
16
 
18
17
  # clients
@@ -35,6 +34,7 @@ require 'sysdig/update_alert'
35
34
 
36
35
  require 'sysdig/get_alert_notification'
37
36
  require 'sysdig/get_alert_notifications'
37
+ require 'sysdig/update_alert_notification'
38
38
  require 'sysdig/alert_notification'
39
39
  require 'sysdig/alert_notifications'
40
40
 
data/lib/sysdig/alert.rb CHANGED
@@ -18,8 +18,8 @@ class Sysdig::Alert < Sysdig::Model
18
18
  attribute :segment_condition, alias: "segmentCondition"
19
19
  attribute :severity, type: :integer
20
20
  attribute :targets, type: :array
21
- attribute :timespan, parser: lambda { |v, _| i = v.to_i; i > 1_000_000 ? i / 1_000_000 : i }
22
- attribute :type, parser: lambda { |v, _| v.to_s.upcase }
21
+ attribute :timespan, parser: method(:microsecond_datetime)
22
+ attribute :type, parser: method(:upcase)
23
23
  attribute :version, type: :integer
24
24
 
25
25
  def destroy
@@ -16,6 +16,6 @@ class Sysdig::AlertFilter
16
16
  end
17
17
 
18
18
  def self.normalize_condition(string)
19
- string.gsub(/(^\\?\")|(\\?\"$)/, "")
19
+ string.gsub(/(^\\?['"])|(\\?['"]$)/, "")
20
20
  end
21
21
  end
@@ -1,17 +1,56 @@
1
1
  class Sysdig::AlertNotification < Sysdig::Model
2
2
 
3
+ # @arg v String like "container.name = 'deis-builder' and agent.tag.id = '8dbf0cfb-7b4b-42ea-b1f1-b742a5bdebf4'"
4
+ def self.load_filter(s, _)
5
+ case s
6
+ when String
7
+ s.split(" and ").
8
+ map { |c| c.split(" = ") }.
9
+ inject({}) { |r,(k,v)| r.merge(k => Sysdig::AlertFilter.normalize_condition(v)) }
10
+ else s
11
+ end
12
+ end
13
+
14
+ def self.dump_filter(h)
15
+ case h
16
+ when Hash
17
+ h.each_with_object([]) { |(k,v),r|
18
+ r << "#{k} = #{Sysdig::AlertFilter.normalize_condition(v).inspect}"
19
+ }.join(" and ")
20
+ else h
21
+ end
22
+ end
23
+
3
24
  identity :id, type: :integer
4
25
 
5
- attribute :timespan, type: :integer
6
- attribute :severity, type: :integer
7
- attribute :target
8
- attribute :alert_id, type: :integer, alias: "alert"
9
- attribute :type
10
- attribute :criteria
11
- attribute :resolved, type: :boolean
26
+ attribute :alert_id, type: :integer, alias: "alert"
12
27
  attribute :condition
13
- attribute :state
14
- attribute :group_aggregations, type: :array, alias: "groupAggregations"
15
- attribute :entities, type: :array
28
+ attribute :entities, type: :array # @todo map to object
29
+ attribute :filter, parser: method(:load_filter)
30
+ attribute :resolved, type: :boolean
31
+ attribute :severity, type: :integer
32
+ attribute :state, parser: method(:upcase)
33
+ attribute :timespan, parser: method(:microsecond_datetime)
34
+ attribute :timestamp, parser: method(:epoch_time), divisor: 1_000_000
35
+
36
+ # @todo get target information out of an alert notification
37
+ def save
38
+ params = {
39
+ "alert" => self.alert_id,
40
+ "filter" => self.class.dump_filter(self.filter),
41
+ "resolved" => self.resolved,
42
+ "severity" => self.severity,
43
+ "state" => self.state,
44
+ "timestamp" => self.timestamp.to_i * 1_000_000,
45
+ }
46
+
47
+ data = service.update_alert_notification(self.identity, params).body.fetch("notification")
48
+
49
+ merge_attributes(data)
50
+ end
16
51
 
52
+ def resolve!
53
+ self.resolved = true
54
+ self.save
55
+ end
17
56
  end
@@ -6,6 +6,12 @@ class Sysdig::AlertNotifications < Sysdig::Collection
6
6
  to = options[:to] || Time.now
7
7
  from = options[:from] || Time.at(Time.now.to_i - 86400) # 1 day
8
8
 
9
- load(service.get_notifications(from, to).body.fetch("notifications"))
9
+ load(service.get_alert_notifications(from, to).body.fetch("notifications"))
10
+ end
11
+
12
+ def get(identity)
13
+ new(
14
+ service.get_alert_notification(identity).body.fetch("notification")
15
+ )
10
16
  end
11
17
  end
@@ -1,7 +1,15 @@
1
1
  class Sysdig::GetAlertNotification < Sysdig::Request
2
2
  def real(notification_id)
3
3
  service.request(
4
- :path => "/api/notifications/#{notification_id}",
4
+ :path => File.join("/api/notifications", notification_id.to_s),
5
+ )
6
+ end
7
+
8
+ def mock(notification_id)
9
+ service.response(
10
+ :body => {
11
+ "notification" => service.data[:alert_notifications].fetch(notification_id)
12
+ },
5
13
  )
6
14
  end
7
15
  end
@@ -1,11 +1,26 @@
1
1
  class Sysdig::GetAlertNotifications < Sysdig::Request
2
2
  def real(from, to)
3
- from_i = from.to_i * 1_000_000
4
- to_i = to.to_i * 1_000_000
3
+ from_i, to_i = timestamps(from, to)
5
4
 
6
5
  service.request(
7
6
  :path => "/api/notifications",
8
7
  :params => {"from" => from_i, "to" => to_i},
9
8
  )
10
9
  end
10
+
11
+ def timestamps(*args)
12
+ args.map { |a| a.to_i * 1_000_000 }
13
+ end
14
+
15
+ def mock(from, to)
16
+ from_i, to_i = timestamps(from, to)
17
+
18
+ notifications = service.data[:alert_notifications].values.select { |an|
19
+ an["timestamp"] >= from_i && an["timestamp"] <= to_i
20
+ }
21
+
22
+ service.response(
23
+ :body => { "notifications" => notifications },
24
+ )
25
+ end
11
26
  end
data/lib/sysdig/mock.rb CHANGED
@@ -9,6 +9,7 @@ class Sysdig::Mock
9
9
  @@data ||= Hash.new { |h,url|
10
10
  h[url] = {
11
11
  :alerts => {},
12
+ :alert_notifications => {},
12
13
  :user_notifications => {
13
14
  "email" => {
14
15
  "enabled" => false,
@@ -75,4 +76,27 @@ class Sysdig::Mock
75
76
  @id ||= 0
76
77
  @id += 1
77
78
  end
79
+
80
+ def create_alert_notification(alert, state: "ACTIVE", resolved: false)
81
+ alert_notification_id = self.serial_id
82
+ alert_data = self.data[:alerts].fetch(alert.identity)
83
+
84
+ alert_notification = self.data[:alert_notifications][alert_notification_id] = {
85
+ "id" => alert_notification_id,
86
+ "timestamp" => (Time.now.to_i - rand(60..300)) * 1_000_000,
87
+ "timespan" => alert_data["timespan"].to_i,
88
+ "filter" => alert_data["filter"],
89
+ "condition" => alert_data["condition"],
90
+ "state" => state.upcase,
91
+ "resolved" => resolved,
92
+ "alert" => alert.identity.to_s,
93
+ "entities" => [
94
+ "filter" => alert_data["filter"],
95
+ "target" => {}, # @fixme the hell
96
+ "metricValues" => {}, # @fixme breakdown of condition
97
+ ]
98
+ }
99
+
100
+ self.alert_notifications.new(alert_notification)
101
+ end
78
102
  end
data/lib/sysdig/model.rb CHANGED
@@ -1,5 +1,21 @@
1
1
  class Sysdig::Model
2
- def self.epoch_time(v, _)
3
- Time.at(v / 1000)
2
+ def self.epoch_time(v, options={})
3
+ divisor = options[:divisor] || 1_000
4
+
5
+ case v
6
+ when Time then v
7
+ when Numeric then Time.at(v / divisor)
8
+ else nil
9
+ end
10
+ end
11
+
12
+ def self.microsecond_datetime(v, *)
13
+ i = v.to_i
14
+
15
+ i > 1_000_000 ? i / 1_000_000 : i
16
+ end
17
+
18
+ def self.upcase(v, *)
19
+ v.nil? ? v : v.to_s.upcase
4
20
  end
5
21
  end
@@ -0,0 +1,45 @@
1
+ class Sysdig::UpdateAlertNotification < Sysdig::Request
2
+ def self.params
3
+ %w[alert condition entities filter resolved severity state timespan timestamp]
4
+ end
5
+
6
+ def slice(notification)
7
+ Cistern::Hash.slice(notification, *Cistern::Hash.stringify_keys(self.class.params))
8
+ end
9
+
10
+ # "notification"=>
11
+ # {"timestamp"=>1445878320000000,
12
+ # "severity"=>2,
13
+ # "filter"=>
14
+ # "container.name = 'deis-builder' and agent.tag.id = '8dbf0cfb-7b4b-42ea-b1f1-b742a5bdebf4'",
15
+ # "timespan"=>60000000,
16
+ # "condition"=>"timeAvg(uptime) = 0",
17
+ # "entities"=>
18
+ # [{"filter"=>
19
+ # "container.name = 'deis-builder' and agent.tag.id = '8dbf0cfb-7b4b-42ea-b1f1-b742a5bdebf4'",
20
+ # "target"=>
21
+ # {"id"=>"group@agent_tag_id-8dbf0cfb-7b4b-42ea-b1f1-b742a5bdebf4",
22
+ # "type"=>"GROUP",
23
+ # "subTarget"=>[{"metric"=>"container.name", "value"=>"deis-builder"}]},
24
+ # "metricValues"=>
25
+ # [{"metric"=>"uptime", "aggregation"=>"timeAvg", "value"=>0}]}],
26
+ # "state"=>"ACTIVE",
27
+ # "resolved"=>true,
28
+ # "alert"=>"31586"}}
29
+ def real(notification_id, notification)
30
+ service.request(
31
+ :method => :put,
32
+ :path => File.join("/api/notifications", notification_id.to_s),
33
+ :body => { "notification" => self.slice(notification) },
34
+ )
35
+ end
36
+
37
+ def mock(notification_id, notification)
38
+ update_notification = self.slice(notification)
39
+ existing_notification = service.data[:alert_notifications].fetch(notification_id)
40
+
41
+ service.response(
42
+ :body => { "notification" => existing_notification.merge!(update_notification) },
43
+ )
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  class Sysdig
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sysdig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
11
+ date: 2015-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -167,6 +167,7 @@ files:
167
167
  - lib/sysdig/real.rb
168
168
  - lib/sysdig/response.rb
169
169
  - lib/sysdig/update_alert.rb
170
+ - lib/sysdig/update_alert_notification.rb
170
171
  - lib/sysdig/update_user_notifications.rb
171
172
  - lib/sysdig/user_notifications.rb
172
173
  - lib/sysdig/version.rb