Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
252 lines (251 sloc) 6.38 KB
type host_info =
{ hostname : string;
os_name : string;
cpu_arch : string;
timestamp : Time.t;
};;
#part 1
#require "core_extended";;
open Core_extended.Std;;
let my_host =
let sh = Shell.sh_one_exn in
{ hostname = sh "hostname";
os_name = sh "uname -s";
cpu_arch = sh "uname -p";
timestamp = Time.now ();
};;
#part 2
my_host.cpu_arch;;
#part 3
type 'a timestamped = { item: 'a; time: Time.t };;
#part 4
let first_timestamped list =
List.reduce list ~f:(fun a b -> if a.time < b.time then a else b)
;;
#part 5
let host_info_to_string { hostname = h; os_name = os;
cpu_arch = c; timestamp = ts;
} =
sprintf "%s (%s / %s, on %s)" h os c (Time.to_sec_string ts);;
host_info_to_string my_host;;
#part 6
type host_info =
{ hostname : string;
os_name : string;
cpu_arch : string;
os_release : string;
timestamp : Time.t;
} ;;
#part 7
#warnings "+9";;
let host_info_to_string { hostname = h; os_name = os;
cpu_arch = c; timestamp = ts;
} =
sprintf "%s (%s / %s, on %s)" h os c (Time.to_sec_string ts);;
#part 8
let host_info_to_string { hostname = h; os_name = os;
cpu_arch = c; timestamp = ts; _
} =
sprintf "%s (%s / %s, on %s)" h os c (Time.to_sec_string ts);;
#part 9
let host_info_to_string { hostname; os_name; cpu_arch; timestamp; _ } =
sprintf "%s (%s / %s) <%s>" hostname os_name cpu_arch
(Time.to_string timestamp);;
#part 10
let my_host =
let sh cmd = Shell.sh_one_exn cmd in
let hostname = sh "hostname" in
let os_name = sh "uname -s" in
let cpu_arch = sh "uname -p" in
let os_release = sh "uname -r" in
let timestamp = Time.now () in
{ hostname; os_name; cpu_arch; os_release; timestamp };;
#part 11
let create_host_info ~hostname ~os_name ~cpu_arch ~os_release =
{ os_name; cpu_arch; os_release;
hostname = String.lowercase hostname;
timestamp = Time.now () };;
#part 12
let create_host_info
~hostname:hostname ~os_name:os_name
~cpu_arch:cpu_arch ~os_release:os_release =
{ os_name = os_name;
cpu_arch = cpu_arch;
os_release = os_release;
hostname = String.lowercase hostname;
timestamp = Time.now () };;
#part 13
type log_entry =
{ session_id: string;
time: Time.t;
important: bool;
message: string;
}
type heartbeat =
{ session_id: string;
time: Time.t;
status_message: string;
}
type logon =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
;;
#part 14
let get_session_id t = t.session_id;;
#part 15
let get_heartbeat_session_id (t:heartbeat) = t.session_id;;
#part 16
let status_and_session t = (t.status_message, t.session_id);;
let session_and_status t = (t.session_id, t.status_message);;
let session_and_status (t:heartbeat) = (t.session_id, t.status_message);;
#part 17
module Log_entry = struct
type t =
{ session_id: string;
time: Time.t;
important: bool;
message: string;
}
end
module Heartbeat = struct
type t =
{ session_id: string;
time: Time.t;
status_message: string;
}
end
module Logon = struct
type t =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
end;;
#part 18
let create_log_entry ~session_id ~important message =
{ Log_entry.time = Time.now (); Log_entry.session_id;
Log_entry.important; Log_entry.message }
;;
#part 19
let create_log_entry ~session_id ~important message =
{ Log_entry.
time = Time.now (); session_id; important; message }
;;
#part 20
let message_to_string { Log_entry.important; message; _ } =
if important then String.uppercase message else message
;;
#part 21
let is_important t = t.Log_entry.important;;
#part 22
type client_info =
{ addr: Unix.Inet_addr.t;
port: int;
user: string;
credentials: string;
last_heartbeat_time: Time.t;
};;
let register_heartbeat t hb =
{ addr = t.addr;
port = t.port;
user = t.user;
credentials = t.credentials;
last_heartbeat_time = hb.Heartbeat.time;
};;
#part 23
let register_heartbeat t hb =
{ t with last_heartbeat_time = hb.Heartbeat.time };;
#part 24
type client_info =
{ addr: Unix.Inet_addr.t;
port: int;
user: string;
credentials: string;
last_heartbeat_time: Time.t;
last_heartbeat_status: string;
};;
#part 25
let register_heartbeat t hb =
{ t with last_heartbeat_time = hb.Heartbeat.time;
last_heartbeat_status = hb.Heartbeat.status_message;
};;
#part 26
type client_info =
{ addr: Unix.Inet_addr.t;
port: int;
user: string;
credentials: string;
mutable last_heartbeat_time: Time.t;
mutable last_heartbeat_status: string;
};;
#part 27
let register_heartbeat t hb =
t.last_heartbeat_time <- hb.Heartbeat.time;
t.last_heartbeat_status <- hb.Heartbeat.status_message
;;
#part 28
let get_users logons =
List.dedup (List.map logons ~f:(fun x -> x.Logon.user));;
#part 29
module Logon = struct
type t =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
with fields
end;;
#part 30
let get_users logons = List.dedup (List.map logons ~f:Logon.user);;
#part 31
Field.get Logon.Fields.user;;
#part 32
Field.get;;
#part 33
let show_field field to_string record =
let name = Field.name field in
let field_string = to_string (Field.get field record) in
name ^ ": " ^ field_string
;;
#part 34
let logon = { Logon.
session_id = "26685";
time = Time.now ();
user = "yminsky";
credentials = "Xy2d9W"; }
;;
show_field Logon.Fields.user Fn.id logon;;
show_field Logon.Fields.time Time.to_string logon;;
#part 35
Logon.Fields.iter;;
#part 36
let print_logon logon =
let print to_string field =
printf "%s\n" (show_field field to_string logon)
in
Logon.Fields.iter
~session_id:(print Fn.id)
~time:(print Time.to_string)
~user:(print Fn.id)
~credentials:(print Fn.id)
;;
print_logon logon;;
#part 37
#part 38
#part 39
#part 40
#part 41
#part 42
#part 43
#part 44
#part 45
#part 46
#part 47
#part 48
#part 49
#part 50