From bc3c5a1ffdecf02061933df4fb00ed11be06ce37 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 5 Jul 2020 19:31:02 -0400 Subject: add tarsnap metrics --- modules/tick/files/plugins/tarsnap | 64 +++++++++++++++++++++++++ modules/tick/files/plugins/tarsnap.conf | 4 ++ modules/tick/files/plugins/tarsnap.sudoers | 3 ++ modules/tick/manifests/client/plugin/tarsnap.pp | 18 +++++++ 4 files changed, 89 insertions(+) create mode 100644 modules/tick/files/plugins/tarsnap create mode 100644 modules/tick/files/plugins/tarsnap.conf create mode 100644 modules/tick/files/plugins/tarsnap.sudoers create mode 100644 modules/tick/manifests/client/plugin/tarsnap.pp (limited to 'modules/tick') diff --git a/modules/tick/files/plugins/tarsnap b/modules/tick/files/plugins/tarsnap new file mode 100644 index 0000000..0352cdb --- /dev/null +++ b/modules/tick/files/plugins/tarsnap @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby + +require 'date' + +def parse_archive(archive) + # tozt-daily-2018-12-31_03:34:13-homedoypass + m = archive.match(/ + \A + (?[a-z]+) # tozt + - + (?[a-z]+) # daily + - + (?[0-9]+) # 2018 + - + (?[0-9]+) # 12 + - + (?[0-9]+) # 31 + _ + (?[0-9]+) # 03 + : + (?[0-9]+) # 34 + : + (?[0-9]+) # 13 + - + (?[a-z_]+) # homedoypass + \z + /x) + raise "failed to parse archive name '#{archive}'" if m.nil? + + parsed = m.names.zip(m.captures).to_h + parsed["time"] = Time.new( + m["year"], + m["month"], + m["day"], + m["hour"], + m["minute"], + m["second"] + ) + parsed +end + +archives = %x(sudo tarsnap --list-archives) + .lines + .map(&:chomp) + .map{|s| parse_archive(s)} + +now = Time.now +most_recent_since = {} +archives.each do |archive| + name = archive["name"] + since = now - archive["time"] + + if most_recent_since[name].nil? || since < most_recent_since[name] + most_recent_since[name] = since + end +end + +stats = %x(sudo tarsnap --print-stats) +bytes = stats.chomp.match(/(\d+)\z/).to_s.to_i + +most_recent_since.sort.each do |name, since| + puts "tarsnap,name=#{name} days_since_last_run=#{since.to_f / 60.0 / 60.0 / 24.0}" +end +puts "tarsnap file_size=#{bytes}i" diff --git a/modules/tick/files/plugins/tarsnap.conf b/modules/tick/files/plugins/tarsnap.conf new file mode 100644 index 0000000..6a68c1e --- /dev/null +++ b/modules/tick/files/plugins/tarsnap.conf @@ -0,0 +1,4 @@ +[[inputs.exec]] +commands = ["/etc/telegraf/plugins/tarsnap"] +data_format = "influx" +timeout = "30s" diff --git a/modules/tick/files/plugins/tarsnap.sudoers b/modules/tick/files/plugins/tarsnap.sudoers new file mode 100644 index 0000000..d915bf8 --- /dev/null +++ b/modules/tick/files/plugins/tarsnap.sudoers @@ -0,0 +1,3 @@ +Cmnd_Alias TARSNAP = /usr/bin/tarsnap +telegraf ALL=(ALL) NOPASSWD: TARSNAP +Defaults!TARSNAP !logfile, !syslog, !pam_session diff --git a/modules/tick/manifests/client/plugin/tarsnap.pp b/modules/tick/manifests/client/plugin/tarsnap.pp new file mode 100644 index 0000000..e7b0a5b --- /dev/null +++ b/modules/tick/manifests/client/plugin/tarsnap.pp @@ -0,0 +1,18 @@ +class tick::client::plugin::tarsnap { + file { + "/etc/telegraf/telegraf.d/tarsnap.conf": + source => 'puppet:///modules/tick/plugins/tarsnap.conf', + require => [ + File["/etc/telegraf/telegraf.d"], + File["/etc/telegraf/plugins/tarsnap"], + ], + notify => Service["telegraf"]; + "/etc/telegraf/plugins/tarsnap": + source => 'puppet:///modules/tick/plugins/tarsnap', + mode => '0755', + require => File['/etc/telegraf/plugins']; + "/etc/sudoers.d/telegraf-tarsnap": + source => 'puppet:///modules/tick/plugins/tarsnap.sudoers', + require => Package['sudo']; + } +} -- cgit v1.2.3-54-g00ecf