service-status 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Service::Status
2
+
3
+ [![Build Status](https://travis-ci.org/TheBookPeople/service-status-ruby.svg)](https://travis-ci.org/TheBookPeople/service-status-ruby) [![Code Climate](https://codeclimate.com/github/TheBookPeople/service-status-ruby/badges/gpa.svg)](https://codeclimate.com/github/TheBookPeople/service-status-ruby) [![Test Coverage](https://codeclimate.com/github/TheBookPeople/service-status-ruby/badges/coverage.svg)](https://codeclimate.com/github/TheBookPeople/service-status-ruby) [![Gem Version](https://badge.fury.io/rb/service-status.svg)](http://badge.fury.io/rb/service-status) [![Gem Dependency](https://img.shields.io/gemnasium/TheBookPeople/service-status-ruby.svg)](https://gemnasium.com/TheBookPeople/service-status-ruby)
4
+
5
+
6
+ Provides an object representing the status of a service, suitable for exposing
7
+ as JSON as part of a REST api.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'service-status'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install service-status
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ status = ServiceStatus::Status.new("windows", '3.11', boot_time)
29
+ # boot_time is time the app was started
30
+
31
+ # check that we can connect to redis
32
+ redis_ok = true
33
+ begin
34
+ Redis.new.ping
35
+ rescue
36
+ redis_ok = false
37
+ end
38
+
39
+ status.add_check('redis', redis_ok)
40
+
41
+ JSON.pretty_generate(status) # render as JSON as required
42
+ ```
43
+ ## Format
44
+
45
+ As below. Checks are always part of the 'checks' array. If they have failed,
46
+ they are included in 'errors' as well.
47
+
48
+ 'Status' is either "online" or "offline".
49
+
50
+ ```javascript
51
+ {
52
+ "name": "windows",
53
+ "version": "3.11",
54
+ "hostname": "clippy",
55
+ "errors": [],
56
+ "checks": [
57
+ "redis"
58
+ ],
59
+ "timestamp": "2015-05-07 14:35:17",
60
+ "uptime": "14d:23:11:21",
61
+ "diskusage": "64%",
62
+ "status": "online"
63
+ }
64
+
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/[my-github-username]/service-status/fork )
70
+ 2. git flow init -d (assuming you have git flow)
71
+ 2. Create your feature branch (`git flow feature start my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
74
+ 5. Create a new Pull Request
75
+
76
+ ## Copyright
77
+
78
+ Copyright (c) 2015 The Book People
79
+
80
+ See LICENSE (GPLv3)
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+ require 'service_status/version'
7
+
8
+ desc 'Run Code quality checks and tests '
9
+ task default: [:clean, :rubocop, :test]
10
+
11
+ desc 'Run Code quality checks, tests and then create Gem File'
12
+ task build: [:clean, :rubocop, :test, :gem]
13
+
14
+ CLEAN.include("service-status-#{ServiceStatus::VERSION}.gem")
15
+ CLEAN.include('coverage')
16
+
17
+ task :test do
18
+ RSpec::Core::RakeTask.new(:spec) do |t|
19
+ t.pattern = 'spec/**/*_spec.rb'
20
+ t.verbose = false
21
+ end
22
+
23
+ Rake::Task['spec'].execute
24
+ end
25
+
26
+ task :rubocop do
27
+ RuboCop::RakeTask.new(:rubocop) do |task|
28
+ task.patterns = ['lib/**/*.rb']
29
+ task.fail_on_error = true
30
+ end
31
+ end
32
+
33
+ task :gem do
34
+ system 'gem build service-status.gemspec'
35
+ end
@@ -0,0 +1,93 @@
1
+ require 'service_status/version'
2
+
3
+ require 'pp'
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'socket'
7
+ require 'sys/filesystem'
8
+
9
+ module ServiceStatus
10
+ class Status
11
+ attr_reader :name, :version, :hostname, :errors, :checks, :timestamp
12
+
13
+ def initialize(name, version, boot_time)
14
+ @boot_time = boot_time
15
+ @name = name
16
+ @version = version
17
+ @hostname = Socket.gethostname
18
+ @checks = []
19
+ @timestamp = Time.now.strftime('%Y-%m-%d %T')
20
+ @errors = []
21
+ end
22
+
23
+ def add_check(name, ok)
24
+ @checks << name
25
+ @errors << name unless ok
26
+ end
27
+
28
+ def add_http_get_check(name, url)
29
+ @checks << name
30
+ uri = URI(url)
31
+ begin
32
+ res = Net::HTTP.get_response(uri)
33
+ @errors << name unless res.is_a?(Net::HTTPSuccess)
34
+ rescue
35
+ @errors << name
36
+ end
37
+ end
38
+
39
+ def status
40
+ online? ? 'online' : 'offline'
41
+ end
42
+
43
+ def online?
44
+ @errors.empty?
45
+ end
46
+
47
+ def uptime
48
+ total_seconds = Time.now - @boot_time
49
+ seconds = total_seconds % 60
50
+ minutes = (total_seconds / 60) % 60
51
+ hours = total_seconds / (60 * 60)
52
+ days = total_seconds / (60 * 60 * 24)
53
+ format('%01dd:%02d:%02d:%02d', days, hours, minutes, seconds)
54
+ end
55
+
56
+ def disk_usage
57
+ format('%01d%%', (disk_used / disk_size) * 100)
58
+ end
59
+
60
+ # rubocop:disable MethodLength
61
+ def to_json(*a)
62
+ {
63
+ name: name,
64
+ version: version,
65
+ hostname: hostname,
66
+ errors: errors,
67
+ checks: checks,
68
+ timestamp: timestamp,
69
+ uptime: uptime,
70
+ diskusage: disk_usage,
71
+ status: status
72
+ }.to_json(*a)
73
+ end
74
+
75
+ private
76
+
77
+ def disk_used
78
+ disk_size - disk_available
79
+ end
80
+
81
+ def disk_available
82
+ @blocks_available ||= filesystem.blocks_available.to_f
83
+ end
84
+
85
+ def disk_size
86
+ @disk_size ||= filesystem.blocks.to_f
87
+ end
88
+
89
+ def filesystem
90
+ @stats ||= Sys::Filesystem.stat('/')
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module ServiceStatus
2
+ VERSION = '0.0.4'
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'service_status/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'service-status'
8
+ spec.version = ServiceStatus::VERSION
9
+ spec.authors = ['William Griffiths', 'Luke Farrar']
10
+ spec.email = ['william.griffiths@thebookpeople.co.uk', 'luke.farrar@thebookpeople.co.uk']
11
+ spec.summary = 'Provides a simple JSON service status report.'
12
+ spec.description = 'When monitoring REST servers it is useful to have a standard /status page. This gem provides such content.'
13
+ spec.homepage = 'https://github.com/TheBookPeople/service-status-ruby'
14
+ spec.license = 'GPLv3'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+ spec.add_dependency 'sys-filesystem', '~> 1.1'
23
+ spec.add_development_dependency 'rake', '~> 10.4'
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rspec', '~> 3.1'
26
+ spec.add_development_dependency 'guard-rspec', '~> 4.5'
27
+ spec.add_development_dependency 'rb-inotify', '~> 0.9'
28
+ spec.add_development_dependency 'rb-fsevent', '~> 0.9'
29
+ spec.add_development_dependency 'rb-fchange', '~> 0.0'
30
+ spec.add_development_dependency 'terminal-notifier-guard', '~> 1.6'
31
+ spec.add_development_dependency 'rubocop', '~> 0.28'
32
+ spec.add_development_dependency 'simplecov', '~> 0.9'
33
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
34
+ spec.add_development_dependency 'webmock', '~> 1.21'
35
+ spec.add_development_dependency 'vcr', '~> 2.9'
36
+ spec.add_development_dependency 'timecop', '~> 0.7'
37
+ end