Class: Dropsonde::Metrics::Platforms

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsonde/metrics/platforms.rb

Overview

platforms plugin

Class Method Summary collapse

Class Method Details

.cleanupObject



122
123
124
# File 'lib/dropsonde/metrics/platforms.rb', line 122

def self.cleanup
  # run just after generating this metric
end

.descriptionObject



10
11
12
13
14
15
16
# File 'lib/dropsonde/metrics/platforms.rb', line 10

def self.description
  <<~DESCRIPTION
    This group of metrics generates usage patterns by platform.
    Currently implemented is a list of classes, the platforms
    they are declared on, and a count of each combination.
  DESCRIPTION
end

.exampleObject



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
# File 'lib/dropsonde/metrics/platforms.rb', line 95

def self.example
  # this method is used to generate a table filled with randomized data to
  # make it easier to write data aggregation queries without access to the
  # actual private data that users have submitted.

  platforms = %w[RedHat Debian Windows Suse FreeBSD Darwin Archlinux AIX]
  classes   = ['', '::Config', '::Service', '::Server', '::Client', '::Packages']

  data = Dropsonde::Cache.modules
                         .sample(rand(35))
                         .map { |item|
    name = item.split('-').last.capitalize + classes.sample

    rand(5).times.map do
      {
        name: name,
        platform: platforms.sample,
        count: rand(1000),
      }
    end
  }.flatten

  [
    class_platforms: data.uniq,
  ]
end

.initialize_platformsObject



5
6
7
8
# File 'lib/dropsonde/metrics/platforms.rb', line 5

def self.initialize_platforms
  # require any libraries needed here -- no need to load puppet; it's already initialized
  # All plugins are initialized before any metrics are generated.
end

.run(puppetdb_session = nil) ⇒ Object



55
56
57
58
59
60
61
62
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
# File 'lib/dropsonde/metrics/platforms.rb', line 55

def self.run(puppetdb_session = nil)
  # skip this metric if we don't have an active PuppetDB connection
  return unless puppetdb_session

  classes = puppetdb_session.puppet_db.request('', 'resources[certname, title] { type = "Class" }').data
  facts   = puppetdb_session.puppet_db.request('', 'facts[certname, value] { name = "osfamily" }').data

  # All public Forge modules that are installed.
  modules = Puppet.lookup(:environments).list.map { |env|
    env.modules.select { |mod| mod.forge_module? }.map do |fmod|
      fmod.name
    end
  }.flatten.uniq

  data = classes.map { |item|
    # filter out any that don't come from public Forge modules
    mod = item['title'].split('::').first.downcase
    next unless modules.include? mod

    item['platform'] = facts.find { |fact|
      fact['certname'] == item['certname']
    }['value']

    {
      name: item['title'],
      platform: item['platform'],
    }
  }.compact

  data.each do |item|
    item[:count] = data.select { |i|
      i[:name] == item[:name] and i[:platform] == item[:platform]
    }.count
  end

  [
    class_platforms: data,
  ]
end

.schemaObject



18
19
20
21
22
23
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
# File 'lib/dropsonde/metrics/platforms.rb', line 18

def self.schema
  # return an array of hashes of a partial schema to be merged into the complete schema
  # See https://cloud.google.com/bigquery/docs/schemas#specifying_a_json_schema_file
  [
    {
      "fields": [
        {
          "description": 'The class name name',
          "mode": 'NULLABLE',
          "name": 'name',
          "type": 'STRING',
        },
        {
          "description": 'The osfamily of the node the class is declared on',
          "mode": 'NULLABLE',
          "name": 'platform',
          "type": 'STRING',
        },
        {
          "description": 'The number of time this combination is declared',
          "mode": 'NULLABLE',
          "name": 'count',
          "type": 'INTEGER',
        },
      ],
      "description": "List of all classes in the infrastructure and platforms they're declared on.",
      "mode": 'REPEATED',
      "name": 'class_platforms',
      "type": 'RECORD',
    },
  ]
end

.setupObject



51
52
53
# File 'lib/dropsonde/metrics/platforms.rb', line 51

def self.setup
  # run just before generating this metric
end