log_timer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ffabb4e386351a7abcabbb69ad32f9dcffe1d1f
4
+ data.tar.gz: b171b77fc6be89ebeafcc6bfc20999211cfb6085
5
+ SHA512:
6
+ metadata.gz: 8056db0d5e567d5c7f404e1ab2615abf65ece14dab953967e5414d0d6ec582da35ecf4eda5b232297121adbd35a2cb5dd11c96372c858ff06994595e49ad2893
7
+ data.tar.gz: a341d54fa15efa9e836f7a216152999bc8c55eb94a2b72134ded8b92a591d0c9e8eddc162be9040f51bb22d4c8e49bcd90c6bfb6d940f157d55a559aeb5db3d0
@@ -0,0 +1,10 @@
1
+ /.idea
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at g.visscher@core4.de. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in log_timer.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gerrit Visscher
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # LogTimer
2
+
3
+ If a service crashed silently, chances are, it does not write to its logs for a while. This
4
+ gem checks a list of log files if they changed recently and alerts you if a file is older
5
+ than the given limit.
6
+
7
+ ## Installation
8
+
9
+ $ gem install log_timer
10
+
11
+ ## Usage
12
+
13
+ When you run the gem for the first time, use the `--init` parameter to generate a basic example config.
14
+ Then open ~/.log_timer.yml in your favorite editor (most likely vim) and add the (log) files you want to have checked.
15
+
16
+ When you run log_timer, it will check all files listed in the "files:" section and check their modification times.
17
+ If a file is older than the specified limit, it will warn you with a message. This is not yet as useful as I would want
18
+ to have it. So if you run `log_timer --mail` it will send a mail to you with the same info plus the last lines from
19
+ the file (either 10 or, if specified, the number given with "tail:").
20
+
21
+ A cronjob might look like this:
22
+
23
+ ```crontab
24
+ 0 * * * * log_timer --mail --quiet
25
+ ```
26
+
27
+ The limit works with [chronic_duration](https://github.com/hpoydar/chronic_duration). Most likely you will need
28
+ something like "30d" for 30 days or 24 hours ("24h").
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
33
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
36
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
37
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CORE4/log_timer. This project is intended
42
+ to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
43
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'log_timer'
4
+ require 'optparse'
5
+ require 'active_support/core_ext/hash/keys'
6
+ require 'yaml'
7
+
8
+ cmd_options = {
9
+ config: "#{ENV['HOME']}/.log_timer.yml",
10
+ quiet: false,
11
+ mail: false,
12
+ write_config: false
13
+ }
14
+
15
+ DEFAULT_CONFIG = {
16
+ hostname: `hostname`,
17
+ mail: {
18
+ smtp_server: 'example.com',
19
+ smtp_port: 25,
20
+ smtp_user: 'exmapleuser',
21
+ smtp_password: 'examplepassword',
22
+ from: 'logtimer@example.com',
23
+ to: 'youraddress@example.com'
24
+ },
25
+ files: {
26
+ old: {
27
+ path: '/path/to/some.log',
28
+ limit: '24h',
29
+ tail: 10
30
+ },
31
+ new: {
32
+ path: '/some/other.log',
33
+ limit: '60d'
34
+ }
35
+ }
36
+ }
37
+
38
+ optparse = OptionParser.new do |opts|
39
+ opts.banner = "Usage: log_timer [options]\nDefault config file: #{cmd_options[:config]}"
40
+
41
+ opts.on('-q', '--quiet', 'Do not output anything. (when using with cron, see --mail)') do
42
+ cmd_options[:quiet] = true
43
+ end
44
+
45
+ opts.on('-c [CONFIG]', '--config [CONFIG]', 'Path to the config file') do |config|
46
+ cmd_options[:config] = config
47
+ end
48
+
49
+ opts.on('-m', '--mail', 'Send mail when an old file is found') do
50
+ cmd_options[:mail] = true
51
+ end
52
+
53
+ opts.on('--init', 'Write a new config file with default values') do
54
+ cmd_options[:write_config] = true
55
+ end
56
+ end
57
+ optparse.parse!
58
+
59
+ if cmd_options[:write_config]
60
+ File.rename cmd_options[:config], "#{cmd_options[:config]}.bkp" if File.exist? cmd_options[:config]
61
+
62
+ File.open(cmd_options[:config], 'w') do |file|
63
+ file.write(DEFAULT_CONFIG.deep_stringify_keys.to_yaml)
64
+ end
65
+ puts "Config written into '#{cmd_options[:config]}'." unless cmd_options[:quiet]
66
+ exit 0
67
+ end
68
+
69
+ unless File.exist? cmd_options[:config]
70
+ STDERR.puts "Cannot find config file (#{cmd_options[:config]})"
71
+ exit 1
72
+ end
73
+
74
+ config = YAML.load_file(cmd_options[:config]).deep_symbolize_keys
75
+
76
+ config[:cmd_options] = cmd_options
77
+
78
+ LogTimer::LogTimer.new(config).check
@@ -0,0 +1,47 @@
1
+ require 'log_timer/version'
2
+ require 'log_timer/log'
3
+ require 'log_timer/mailer'
4
+ require 'yaml'
5
+ require 'chronic_duration'
6
+
7
+ require 'pp'
8
+
9
+ module LogTimer
10
+ class LogTimer
11
+ attr_accessor :mailer, :config
12
+
13
+ def initialize(config)
14
+ @config = config
15
+ @mailer = Mailer.new(@config[:mail])
16
+ end
17
+
18
+ def check
19
+ @config[:files].each do |_key, file_info|
20
+ log = Log.new(file_info[:path])
21
+ if log.older?(Time.now - ChronicDuration.parse(file_info[:limit]))
22
+ mailer.send(mail_subject(log), mail_body(log, file_info)) if @config[:cmd_options][:mail]
23
+ puts "#{log.path} is older than the limit #{file_info[:limit]}" unless @config[:cmd_options][:quiet]
24
+ end
25
+ end
26
+ end
27
+
28
+ def mail_subject(log)
29
+ "#{File.basename(log.path)} on #{@config[:hostname] || `hostname`} is getting old"
30
+ end
31
+
32
+ def mail_body(log, file_info)
33
+ tail_limit = file_info[:tail_limit] || 10
34
+ body = <<EOD
35
+ The log file #{log.path} is got older than the specified limit (#{file_info[:limit]}). Maybe a service stopped working.
36
+ The file was last changed on #{log.modification_date}.
37
+
38
+ The last #{tail_limit} lines are:
39
+ #{log.tail(tail_limit)}
40
+
41
+ Sincerely,
42
+ The LogTimer gem
43
+ EOD
44
+ body
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ module LogTimer
2
+ # Represents a log file
3
+ class Log
4
+ attr_accessor :path
5
+
6
+ def initialize(path)
7
+ @path = path
8
+ end
9
+
10
+ def modification_date
11
+ File.mtime(@path)
12
+ end
13
+
14
+ # Checks if the log file is older than the given datetime
15
+ def older?(reference_time)
16
+ File.mtime(@path) < reference_time
17
+ end
18
+
19
+ # Returns the last n lines from the log file
20
+ # Implementing this directly in Ruby is annoyingly complicated (and/or slow) it seems
21
+ def tail(lines = 10)
22
+ `tail -n #{lines} #{@path}`.split("\n")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,61 @@
1
+ require 'net/smtp'
2
+
3
+ module LogTimer
4
+ # Handles sending of mails via smtp or local (mail-utils)
5
+ class Mailer
6
+ attr_accessor :smtp_server, :smtp_port, :smtp_user, :smtp_password, :from, :to
7
+
8
+ def initialize(options)
9
+ @options = options
10
+ end
11
+
12
+ def send(subject, body)
13
+ if @options[:smtp_server] && !@options[:smtp_server].empty?
14
+ send_mail_smtp(compose_message(subject, body))
15
+ else
16
+ send_mail_local(subject, body)
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def compose_message(subject, body)
23
+ to_addresses = [*@options[:to]].join(', ')
24
+ message = <<EOD
25
+ From: Log Notifier <#{@options[:from]}>
26
+ To: <#{to_addresses}>
27
+ Subject: #{subject}
28
+
29
+ #{body}
30
+ EOD
31
+ message
32
+ end
33
+
34
+ def send_mail_smtp(message)
35
+ ::Net::SMTP.start(
36
+ @options[:smtp_server],
37
+ @options[:smtp_port],
38
+ from_domain,
39
+ @options[:smtp_user],
40
+ @options[:smtp_password]) do |smtp|
41
+ smtp.send_message message, @options[:from], @options[:to]
42
+ end
43
+ end
44
+
45
+ def send_mail_local(subject, body)
46
+ stdin, stdout, stderr, wait_thr = Open3.popen3("mail -s \"#{subject.gsub('"', '\"')}\" #{@options[:to].join(', ')}")
47
+ stdin.puts body
48
+
49
+ stdin.close # stdin, stdout and stderr should be closed explicitly in this form.
50
+ stdout.close
51
+ stderr.close
52
+
53
+ wait_thr.value == 0
54
+ end
55
+
56
+ def from_domain
57
+ @options[:from].split('@')[1]
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module LogTimer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'log_timer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'log_timer'
8
+ spec.version = LogTimer::VERSION
9
+ spec.authors = ['Gerrit Visscher']
10
+ spec.email = ['g.visscher@core4.de']
11
+
12
+ spec.summary = 'Checks if log files have been changed recently'
13
+ spec.description = 'If a service crashed silently, chances are, it does not write to its logs for a while. This '\
14
+ 'gem checks a list of log files if they changed recently and alerts you if a file is older '\
15
+ 'than the given limit.'
16
+ spec.homepage = 'https://github.com/CORE4/log_timer'
17
+ spec.license = 'MIT'
18
+
19
+ rejected_files = %w(bin/setup bin/console Rakefile .travis.yml .rspec)
20
+ git_files = `git ls-files -z`.split("\x0")
21
+
22
+ spec.files = git_files.reject { |f| f.match(%r{^(test|spec|features)/}) || rejected_files.include?(f) }
23
+ spec.bindir = 'bin'
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_runtime_dependency 'chronic_duration', '~> 0.10', '>= 0.10.6'
28
+ spec.add_runtime_dependency 'activesupport', '~> 5.0', '>= 5.0.0'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.11'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_timer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gerrit Visscher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chronic_duration
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.10.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.10'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.10.6
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 5.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '5.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 5.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.11'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.11'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ description: If a service crashed silently, chances are, it does not write to its
96
+ logs for a while. This gem checks a list of log files if they changed recently and
97
+ alerts you if a file is older than the given limit.
98
+ email:
99
+ - g.visscher@core4.de
100
+ executables:
101
+ - log_timer
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - bin/log_timer
111
+ - lib/log_timer.rb
112
+ - lib/log_timer/log.rb
113
+ - lib/log_timer/mailer.rb
114
+ - lib/log_timer/version.rb
115
+ - log_timer.gemspec
116
+ homepage: https://github.com/CORE4/log_timer
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.5.1
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Checks if log files have been changed recently
140
+ test_files: []