Skip to content

Commit 167011c

Browse files
authored
Merge pull request #30 from puppetlabs/SLV-672
(SLV-672) Add system class to manage system metric
2 parents 5f97444 + bec1cda commit 167011c

File tree

6 files changed

+174
-8
lines changed

6 files changed

+174
-8
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ node 'master.example.com' {
3737
}
3838
```
3939

40+
Optionally, you can also gather some basic system metrics. Unlike the service metrics, this has to be enabled on each host you want metrics from, and the resulting data will be only on that host. Do not include the top level puppet_metrics_collector on anything other than the master as it will collect the same data as the one on the master. This functionality depends on sysstat.
41+
42+
```
43+
node 'master.example.com' {
44+
include puppet_metrics_collector
45+
include puppet_metrics_collector::system
46+
}
47+
48+
node 'compilerA.example.com', 'compilerB.example.com,' {
49+
include puppet_metrics_collector::system
50+
}
51+
```
52+
4053
### Configuration
4154

4255
This module automatically configures the hosts it queries by querying PuppetDB for PE Infrastructure Hosts. If there is an error with automatic configuration of hosts, refer to [Manual Configuration of Hosts](#manual-configuration-of-hosts).

files/generate_system_metrics

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require "fileutils"
1616
# sar_metric.pp will setup a cron job to run this similar to how pe_metric runs tk_metrics
1717
#
1818
# Example execution
19-
# generate_system_metrics --metric_type cpu --file_interval 300 --polling_interval 1
19+
# generate_system_metrics --metric_type system_cpu --file_interval 300 --polling_interval 1
2020
# --metrics_dir /opt/puppetlabs/puppet-metrics-collector
2121

2222
# General namespace for SystemMetrics module
@@ -27,7 +27,7 @@ module SystemMetrics
2727
#
2828
# @attr [integer] polling_interval Time in seconds between calls to poll the system for data.
2929
# @attr [integer] file_interval Time in seconds between the creation of each output file.
30-
# @attr [string] metric_type cpu|memory
30+
# @attr [string] metric_type system_cpu|system_memory
3131
# @attr [string] metrics_dir The puppet_metrics_collector output directory.
3232
# @attr [boolean] verbose Verbose output
3333
# @attr [string] hostname Name of the host the metrics are from. In directory name and json file.
@@ -40,7 +40,7 @@ module SystemMetrics
4040
#
4141
# @param [integer] polling_interval Time in seconds between calls to poll the system for data.
4242
# @param [integer] file_interval Time in seconds between the creation of each output file.
43-
# @param [string] metric_type cpu|memory
43+
# @param [string] metric_type system_cpu|system_memory
4444
# @param [string] metrics_dir The puppet_metrics_collector output directory.
4545
# @param [boolean] verbose Verbose output
4646
#
@@ -65,7 +65,7 @@ module SystemMetrics
6565
def run_sar
6666
times_to_poll = (@file_interval / @polling_interval).round
6767
# sar inputs are polling interval and how many times to poll
68-
comm_flags = " -r" if @metric_type =~ /memory/
68+
comm_flags = " -r" if @metric_type =~ /system_memory/
6969
comm = "sar #{comm_flags} #{@polling_interval} #{times_to_poll}"
7070
puts "sar command is: #{comm}" if @verbose
7171
%x[#{comm}]
@@ -83,7 +83,7 @@ module SystemMetrics
8383
def parse_sar_output(sar_output)
8484
sar_output_arr = sar_output.split(/\n+|\r+/).reject(&:empty?).map { |line| line.split }
8585

86-
if ( @metric_type == "memory")
86+
if ( @metric_type == "system_memory")
8787
unique_header_str = "%memused"
8888
else
8989
unique_header_str = "%user"
@@ -142,7 +142,7 @@ module SystemMetrics
142142
# @return [void]
143143
def create_file(json_dataset, time_stamp_obj)
144144
filename = time_stamp_obj.utc.strftime('%Y%m%dT%H%M%SZ') + '.json'
145-
dirname = "#{@metrics_dir}/system_#{@metric_type}/#{@hostname}"
145+
dirname = "#{@metrics_dir}/#{@metric_type}/#{@hostname}"
146146
file_path = "#{dirname}/#{filename}"
147147
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
148148
puts "Creating json file: #{file_path}" if @verbose
@@ -167,10 +167,10 @@ end
167167

168168
if $PROGRAM_NAME == __FILE__
169169

170-
VALID_METRIC_TYPES = %w[cpu memory]
170+
VALID_METRIC_TYPES = %w[system_cpu system_memory]
171171
FILE_INTERVAL_DEFAULT = 60 * 5
172172
POLLING_INTERVAL_DEFAULT = 1
173-
METRIC_TYPE_DEFAULT = "cpu"
173+
METRIC_TYPE_DEFAULT = "system_cpu"
174174
METRICS_DIR_DEFAULT = "/opt/puppetlabs/puppet-metrics-collector"
175175

176176
DESCRIPTION = <<-DESCRIPTION

manifests/sar_metric.pp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
define puppet_metrics_collector::sar_metric (
2+
String $output_dir,
3+
String $scripts_dir,
4+
Enum['absent', 'present'] $metric_ensure = 'present',
5+
String $metrics_type = $title,
6+
String $cron_minute = '*/5',
7+
Integer $retention_days = 90,
8+
Integer $polling_frequency_seconds = 1,
9+
Integer $collection_frequency = 5, #minutes
10+
String $metric_script_file = 'generate_system_metrics',
11+
) {
12+
13+
$metrics_output_dir = "${output_dir}/${metrics_type}"
14+
15+
$_metric_ensure = $metric_ensure ? {
16+
'present' => directory,
17+
'absent' => absent,
18+
}
19+
20+
file { $metrics_output_dir :
21+
ensure => $_metric_ensure,
22+
}
23+
24+
$script_file_name = "${scripts_dir}/${metric_script_file}"
25+
$file_interval_seconds = $collection_frequency * 60
26+
27+
$metrics_command = join(["${script_file_name} --metric_type ${metrics_type}",
28+
" --file_interval ${file_interval_seconds}",
29+
" --polling_interval ${polling_frequency_seconds}",
30+
" --metrics_dir ${output_dir}"], '')
31+
32+
cron { "${metrics_type}_metrics_collection" :
33+
ensure => $metric_ensure,
34+
command => $metrics_command,
35+
user => 'root',
36+
minute => $cron_minute,
37+
}
38+
39+
$metrics_tidy_script_path = "${scripts_dir}/${metrics_type}_metrics_tidy"
40+
41+
file { $metrics_tidy_script_path :
42+
ensure => $metric_ensure,
43+
mode => '0744',
44+
content => epp('puppet_metrics_collector/tidy_cron.epp', {
45+
'metrics_output_dir' => $metrics_output_dir,
46+
'metrics_type' => $metrics_type,
47+
'retention_days' => $retention_days,
48+
}),
49+
}
50+
51+
# The hardcoded numbers with the fqdn call are to trigger the tidy to run at a randomly selected
52+
# time between 12:00 AM and 3:00 AM
53+
cron { "${metrics_type}_metrics_tidy" :
54+
ensure => $metric_ensure,
55+
user => 'root',
56+
hour => fqdn_rand(3, $metrics_type),
57+
minute => (5 * fqdn_rand(11, $metrics_type)),
58+
command => $metrics_tidy_script_path
59+
}
60+
}

manifests/system.pp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class puppet_metrics_collector::system (
2+
String $output_dir = '/opt/puppetlabs/puppet-metrics-collector',
3+
Integer $collection_frequency = 5, #minutes
4+
Integer $polling_frequency_seconds = 1,
5+
Integer $retention_days = 90,
6+
String $system_metrics_ensure = present,
7+
Boolean $symlink_puppet_metrics_collector = true,
8+
Boolean $manage_sysstat = true,
9+
) {
10+
$scripts_dir = "${output_dir}/scripts"
11+
$bin_dir = "${output_dir}/bin"
12+
13+
#assume if output is defined, all of the rest will be too as the init.pp must be in use
14+
#and thus we don't need to redefine these
15+
if ! defined(File[$output_dir]) {
16+
file { [ $output_dir, $scripts_dir, $bin_dir]:
17+
ensure => directory,
18+
}
19+
20+
file { "${bin_dir}/puppet-metrics-collector":
21+
ensure => file,
22+
owner => 'root',
23+
group => 'root',
24+
mode => '0755',
25+
content => epp('puppet_metrics_collector/puppet-metrics-collector.epp', {
26+
'output_dir' => $output_dir,
27+
}),
28+
}
29+
30+
$symlink_ensure = $symlink_puppet_metrics_collector ? {
31+
false => 'absent',
32+
true => 'symlink',
33+
}
34+
35+
file { '/opt/puppetlabs/bin/puppet-metrics-collector':
36+
ensure => $symlink_ensure,
37+
target => "${bin_dir}/puppet-metrics-collector",
38+
}
39+
}
40+
41+
file { "${scripts_dir}/generate_system_metrics":
42+
ensure => present,
43+
mode => '0755',
44+
source => 'puppet:///modules/puppet_metrics_collector/generate_system_metrics'
45+
}
46+
47+
if $manage_sysstat {
48+
package { 'sysstat':
49+
ensure => installed,
50+
}
51+
}
52+
53+
include puppet_metrics_collector::system_cpu
54+
include puppet_metrics_collector::system_memory
55+
}

manifests/system_cpu.pp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class puppet_metrics_collector::system_cpu (
2+
Integer $collection_frequency = $puppet_metrics_collector::system::collection_frequency,
3+
Integer $polling_frequency_seconds = $puppet_metrics_collector::system::polling_frequency_seconds,
4+
Integer $retention_days = $puppet_metrics_collector::system::retention_days,
5+
String $metrics_ensure = $puppet_metrics_collector::system::system_metrics_ensure,
6+
) {
7+
Puppet_metrics_collector::Sar_metric {
8+
output_dir => $puppet_metrics_collector::system::output_dir,
9+
scripts_dir => $puppet_metrics_collector::system::scripts_dir,
10+
cron_minute => "*/${collection_frequency}",
11+
collection_frequency => $collection_frequency,
12+
polling_frequency_seconds => $polling_frequency_seconds,
13+
retention_days => $retention_days,
14+
}
15+
16+
puppet_metrics_collector::sar_metric { 'system_cpu' :
17+
metric_ensure => $metrics_ensure,
18+
}
19+
}

manifests/system_memory.pp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class puppet_metrics_collector::system_memory (
2+
Integer $collection_frequency = $puppet_metrics_collector::system::collection_frequency,
3+
Integer $polling_frequency_seconds = $puppet_metrics_collector::system::polling_frequency_seconds,
4+
Integer $retention_days = $puppet_metrics_collector::system::retention_days,
5+
String $metrics_ensure = $puppet_metrics_collector::system::system_metrics_ensure,
6+
) {
7+
Puppet_metrics_collector::Sar_metric {
8+
output_dir => $puppet_metrics_collector::system::output_dir,
9+
scripts_dir => $puppet_metrics_collector::system::scripts_dir,
10+
cron_minute => "*/${collection_frequency}",
11+
collection_frequency => $collection_frequency,
12+
polling_frequency_seconds => $polling_frequency_seconds,
13+
retention_days => $retention_days,
14+
}
15+
16+
puppet_metrics_collector::sar_metric { 'system_memory' :
17+
metric_ensure => $metrics_ensure,
18+
}
19+
}

0 commit comments

Comments
 (0)