Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pkg/*
.bundle
.yardoc
doc/*
Gemfile.lock
Gemfile.lock
.idea/
1 change: 1 addition & 0 deletions lib/monit.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "monit/constants"
require "monit/httpd"
require "monit/platform"
require "monit/server"
Expand Down
61 changes: 61 additions & 0 deletions lib/monit/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Constants from monit sources:
# - monit.h
# - event.h
module Monit
MONITOR_NOT = 0
MONITOR_YES = 1
MONITOR_INIT = 2
MONITOR_WAITING = 4

MONITORMODE_ACTIVE = 0
MONITORMODE_PASSIVE = 1
MONITORMODE_MANUAL = 2

PENDINGACTION_IGNORE = 0
PENDINGACTION_ALERT = 1
PENDINGACTION_RESTART = 2
PENDINGACTION_STOP = 3
PENDINGACTION_EXEC = 4
PENDINGACTION_UNMONITOR = 5
PENDINGACTION_START = 6
PENDINGACTION_MONITOR = 7

STATE_SUCCEEDED = 0
STATE_FAILED = 1
STATE_CHANGED = 2
STATE_CHANGEDNOT = 3
STATE_INIT = 4

SERVICE_TYPE_FILESYSTEM = 0
SERVICE_TYPE_DIRECTORY = 1
SERVICE_TYPE_FILE = 2
SERVICE_TYPE_PROCESS = 3
SERVICE_TYPE_HOST = 4
SERVICE_TYPE_SYSTEM = 5
SERVICE_TYPE_FIFO = 6
SERVICE_TYPE_PROGRAM = 7

STATUS_CHECKSUM = 0x1
STATUS_RESOURCE = 0x2
STATUS_TIMEOUT = 0x4
STATUS_TIMESTAMP = 0x8
STATUS_SIZE = 0x10
STATUS_CONNECTION = 0x20
STATUS_PERMISSION = 0x40
STATUS_UID = 0x80
STATUS_GID = 0x100
STATUS_NONEXIST = 0x200
STATUS_INVALID = 0x400
STATUS_DATA = 0x800
STATUS_EXEC = 0x1000
STATUS_FSFLAG = 0x2000
STATUS_ICMP = 0x4000
STATUS_CONTENT = 0x8000
STATUS_INSTANCE = 0x10000
STATUS_ACTION = 0x20000
STATUS_PID = 0x40000
STATUS_PPID = 0x80000
STATUS_HEARTBEAT = 0x100000
STATUS_STATUS = 0x200000
STATUS_UPTIME = 0x40000
end
10 changes: 10 additions & 0 deletions lib/monit/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ def do(action)
end
end

def has_errors?(code = nil, exact = false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like passing in code is done purely for testing purposes; it'd be better to not do so and instead find some other way to set the status, probably by mocking a Service object.

Also I don't quite understand why exact was added; what's wrong with always being exact, or always being fuzzy?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing code to method allows error checking, because monit stores codes in status and error flags in status_hint.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, then; how does code allow error checking? Shouldn't has_errors? already have all the information it needs between the Service object and the constants you've added?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Monit has many errors flags. Argument code allows testing of special error codes. If you call has_errors? without arguments then it just determine presence of errors.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I get it now, thanks for explaining.

I guess I prefer the ActiveRecord way of doing things. .valid? doesn't let you assert against specific errors but rather only generally if there was an error. To check against specific errors, you have to check .errors

I suppose it's just personal preference, though.

if code.nil? or code == 0
self.status.to_i != 0 and self.status_hint.to_i == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than converting status and status_hint to ints constantly, I think they should just be stored as integers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may break interface for other users.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but that's what SemVer is for right? I suppose it's up to the maintainer (if he's still active) whether or not he wants to introduce a breaking change.

elsif exact
self.status.to_i == code and self.status_hint.to_i == 0
else
(self.status.to_i & code) != 0 and (self.status_hint.to_i & code) == 0
end
end

private
# Renames the Service type from "type" to "service_type" to avoid conflicts
def rename_service_type(hash)
Expand Down
30 changes: 30 additions & 0 deletions spec/monit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,35 @@
service.do(:start).should == false
end
end

describe "status checking" do
it "should not has errors" do
service.has_errors?.should == false
service.has_errors?(0).should == false
service.has_errors?(0, true).should == false
end

it "should has execution failed errors" do
# execution failed errors
service.status = "4608"
service.status_hint = "0"

service.has_errors?.should == true
service.has_errors?(Monit::STATUS_EXEC).should == true
service.has_errors?(Monit::STATUS_NONEXIST).should == true
service.has_errors?(Monit::STATUS_EXEC | Monit::STATUS_NONEXIST).should == true
service.has_errors?(Monit::STATUS_EXEC, true).should == false
service.has_errors?(Monit::STATUS_EXEC | Monit::STATUS_NONEXIST, true).should == true
end

it "should has non-error status" do
# execution failed errors
service.status = Monit::STATUS_PID.to_s
service.status_hint = Monit::STATUS_PID.to_s

service.has_errors?.should == false
service.has_errors?(Monit::STATUS_PID).should == false
end
end
end
end