@@ -27,31 +27,49 @@ let ipmitool_bin = "/usr/bin/ipmitool"
2727
2828let ipmitool args =
2929 (* we connect to the local /dev/ipmi0 if available to read measurements from local BMC *)
30- ipmitool_bin :: " -I" :: " open" :: args |> String. concat " "
30+ ipmitool_bin :: args |> String. concat " "
31+
32+ type discovery_error = Devices_missing
33+
34+ let discovery_error_to_string = function
35+ | Devices_missing ->
36+ " IPMI devices are missing"
3137
3238let discover () =
39+ let read_out_line line =
40+ (* this code runs once on startup, logging all the output here will be useful for debugging *)
41+ D. debug " DCMI discover: %s" line ;
42+ let line = String. trim line in
43+ if String. equal line " Power management available" then
44+ Some ()
45+ else
46+ None
47+ in
48+ let read_err_line line =
49+ (* this code runs once on startup, logging all the output here will be useful for debugging *)
50+ D. debug " DCMI discover: %s" line ;
51+ let line = String. trim line in
52+ if String. starts_with ~prefix: " Could not open device at" line then
53+ Some Devices_missing
54+ else
55+ None
56+ in
3357 Utils. exec_cmd
3458 (module Process. D )
3559 ~cmdstring: (ipmitool [" dcmi" ; " discover" ])
36- ~f: (fun line ->
37- (* this code runs once on startup, logging all the output here will be useful for debugging *)
38- D. debug " DCMI discover: %s" line ;
39- if String. trim line = " Power management available" then
40- Some ()
41- else
42- None
43- )
60+ ~read_out_line ~read_err_line
4461
4562let get_dcmi_power_reading () =
63+ let read_out_line line =
64+ (* example line: ' Instantaneous power reading: 34 Watts' *)
65+ try Scanf. sscanf line " Instantaneous power reading : %f Watts" Option. some
66+ with _ -> None
67+ in
68+ let read_err_line _ = None in
4669 Utils. exec_cmd
4770 (module Process. D )
4871 ~cmdstring: (ipmitool [" dcmi" ; " power" ; " reading" ])
49- ~f: (fun line ->
50- (* example line: ' Instantaneous power reading: 34 Watts' *)
51- try
52- Scanf. sscanf line " Instantaneous power reading : %f Watts" Option. some
53- with Scanf. Scan_failure _ | End_of_file -> None
54- )
72+ ~read_out_line ~read_err_line
5573
5674let gen_dcmi_power_reading value =
5775 ( Rrd. Host
@@ -63,18 +81,23 @@ let gen_dcmi_power_reading value =
6381
6482let generate_dss () =
6583 match get_dcmi_power_reading () with
66- | watts :: _ ->
84+ | watts :: _ , _ ->
6785 [gen_dcmi_power_reading watts]
6886 | _ ->
6987 []
7088
7189let _ =
7290 initialise () ;
7391 match discover () with
74- | [] ->
75- D. info " IPMI DCMI power reading is unavailable" ;
76- exit 1
77- | _ ->
92+ | () :: _ , _ ->
7893 D. info " IPMI DCMI power reading is available" ;
7994 main_loop ~neg_shift: 0.5 ~target: (Reporter. Local 1 )
8095 ~protocol: Rrd_interface. V2 ~dss_f: generate_dss
96+ | [] , errs ->
97+ let reason =
98+ List. nth_opt errs 0
99+ |> Option. map discovery_error_to_string
100+ |> Option. value ~default: " unknown"
101+ in
102+ D. warn " IPMI DCMI power readings not available, stopping. Reason: %s"
103+ reason
0 commit comments