-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.ml
49 lines (47 loc) · 1.6 KB
/
action.ml
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
(* Action *)
open Util;;
open Target;;
let perform_action e =
List.iter
begin function
| Mail(what, whom) ->
List.iter
begin fun who ->
let subject = sf "[Monitor] %s is %s" what (if e.is_good then "up" else "down") in
let contact = !Opt.contact in
let cmd =
let b = Buffer.create 80 in
Buffer.add_substitute b
begin function
| "WHO" -> Filename.quote who
| "SUBJECT" -> Filename.quote subject
| "CONTACT" -> Filename.quote !Opt.contact
| x -> msg (sf "Ignoring invalid substitution variable %S" x);
""
end
!Opt.sendmail;
Buffer.contents b
in
let oc = Unix.open_process_out cmd in
fp oc "To: %s\n" who;
fp oc "Subject: %s\n" subject;
fp oc "\n";
fp oc "This is an automatic monitoring script.\n";
fp oc "Contact %s if there are any problems with it.\n" contact;
fp oc "\n";
fp oc "--\n";
fp oc "Monitor\n";
fp oc ".\n";
match Unix.close_process_out oc with
| Unix.WEXITED 0 -> ()
| _ -> msg (sf "Problem mailing to %s." who)
end
whom
| Echo x -> msg (sf "Echo: %S" x)
| Exec cmd ->
msg (sf "Executing %S" cmd);
let r = Sys.command cmd in
msg (sf "Result is %d" r)
end
e.action
;;