/
heartbeat-oss.rb
101 lines (91 loc) · 2.66 KB
/
heartbeat-oss.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class HeartbeatOss < Formula
desc "Lightweight Shipper for Uptime Monitoring"
homepage "https://www.elastic.co/products/beats/heartbeat"
url "https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-oss-7.10.2-darwin-x86_64.tar.gz?tap=elastic/homebrew-tap"
version "7.10.2"
sha256 "82d751b0bca221baeea5e837b6d37f323f430467c81c11f9add9262cc15e6afc"
conflicts_with "heartbeat"
conflicts_with "heartbeat-full"
bottle :unneeded
def install
["fields.yml", "ingest", "kibana", "module"].each { |d| libexec.install d if File.exist?(d) }
(libexec/"bin").install "heartbeat"
(etc/"heartbeat").install "heartbeat.yml"
(etc/"heartbeat").install "modules.d" if File.exist?("modules.d")
(bin/"heartbeat").write <<~EOS
#!/bin/sh
exec #{libexec}/bin/heartbeat \
--path.config #{etc}/heartbeat \
--path.data #{var}/lib/heartbeat \
--path.home #{libexec} \
--path.logs #{var}/log/heartbeat \
"$@"
EOS
end
def post_install
(var/"lib/heartbeat").mkpath
(var/"log/heartbeat").mkpath
end
plist_options :manual => "heartbeat"
def plist; <<~EOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>#{plist_name}</string>
<key>Program</key>
<string>#{opt_bin}/heartbeat</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOS
end
test do
require "socket"
server = TCPServer.new(0)
port = server.addr[1]
(testpath/"config/heartbeat.yml").write <<~EOS
heartbeat.monitors:
- type: tcp
schedule: '@every 5s'
hosts: ["localhost:#{port}"]
check.send: "r u there\\n"
check.receive: "i am here\\n"
output.file:
path: "#{testpath}/heartbeat"
filename: heartbeat
codec.format:
string: '%{[monitor]}'
EOS
pid = fork do
exec bin/"heartbeat", "-path.config", testpath/"config", "-path.data",
testpath/"data"
end
sleep 5
t = nil
begin
t = Thread.new do
loop do
client = server.accept
line = client.readline
if line == "r u there\n"
client.puts("i am here\n")
else
client.puts("goodbye\n")
end
client.close
end
end
sleep 5
assert_match "\"status\":\"up\"", (testpath/"heartbeat/heartbeat").read
ensure
Process.kill "SIGINT", pid
Process.wait pid
t.exit
server.close
end
end
end