|
11 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | 12 | * GNU Lesser General Public License for more details. |
13 | 13 | *) |
14 | | -open Http |
15 | | -open Printf |
16 | | -open Xapi_stdext_pervasives.Pervasiveext |
17 | | -open Xapi_stdext_std.Xstringext |
18 | | -open Forkhelpers |
19 | 14 |
|
20 | | -let content_type = "application/data" |
| 15 | +module Request = Http.Request |
21 | 16 |
|
22 | | -let xen_bugtool = "/usr/sbin/xen-bugtool" |
| 17 | +let finally = Xapi_stdext_pervasives.Pervasiveext.finally |
23 | 18 |
|
24 | 19 | let task_label = "Retrieving system status" |
25 | 20 |
|
26 | | -let module_key = "system_status" |
| 21 | +module L = Debug.Make (struct let name = __MODULE__ end) |
27 | 22 |
|
28 | | -module D = Debug.Make (struct let name = module_key end) |
| 23 | +module Output = struct |
| 24 | + (** The output formats of xen-bugtool *) |
| 25 | + type t = Tar | TarBz2 | Zip |
29 | 26 |
|
30 | | -open D |
| 27 | + let of_string = function |
| 28 | + | "tar" -> |
| 29 | + Some Tar |
| 30 | + | "tar.bz2" -> |
| 31 | + Some TarBz2 |
| 32 | + | "zip" -> |
| 33 | + Some Zip |
| 34 | + | _ -> |
| 35 | + None |
31 | 36 |
|
32 | | -let get_capabilities () = |
33 | | - let cmd = sprintf "%s --capabilities" xen_bugtool in |
34 | | - Helpers.get_process_output cmd |
| 37 | + let to_extension = function |
| 38 | + | Tar -> |
| 39 | + "tar" |
| 40 | + | TarBz2 -> |
| 41 | + "tar.bz2" |
| 42 | + | Zip -> |
| 43 | + "zip" |
35 | 44 |
|
36 | | -(* This fn outputs xen-bugtool straight to the socket, only |
37 | | - for tar output. It should work on embedded edition *) |
38 | | -let send_via_fd __context s entries output = |
39 | | - let s_uuid = Uuidx.to_string (Uuidx.make ()) in |
40 | | - let params = |
| 45 | + let to_mime = function |
| 46 | + | Tar -> |
| 47 | + "appliation/x-tar" |
| 48 | + | TarBz2 -> |
| 49 | + "application/x-bzip2" |
| 50 | + | Zip -> |
| 51 | + "application/zip" |
| 52 | +end |
| 53 | + |
| 54 | +module Bugtool = struct |
| 55 | + let path = "/usr/sbin/xen-bugtool" |
| 56 | + |
| 57 | + let params_cp ~entries ~extension = |
41 | 58 | [ |
42 | | - sprintf "--entries=%s" entries |
| 59 | + Printf.sprintf "--entries=%s" entries |
43 | 60 | ; "--silent" |
44 | 61 | ; "--yestoall" |
45 | | - ; sprintf "--output=%s" output |
46 | | - ; "--outfd=" ^ s_uuid |
| 62 | + ; Printf.sprintf "--output=%s" extension |
47 | 63 | ] |
48 | | - in |
49 | | - let cmd = sprintf "%s %s" xen_bugtool (String.concat " " params) in |
50 | | - debug "running %s" cmd ; |
51 | | - try |
52 | | - let headers = |
53 | | - Http.http_200_ok ~keep_alive:false ~version:"1.0" () |
54 | | - @ [ |
55 | | - "Server: " ^ Xapi_version.xapi_user_agent |
56 | | - ; Http.Hdr.content_type ^ ": " ^ content_type |
57 | | - ; "Content-Disposition: attachment; filename=\"system_status.tgz\"" |
58 | | - ] |
59 | | - in |
60 | | - Http_svr.headers s headers ; |
61 | | - let result = |
62 | | - with_logfile_fd "get-system-status" (fun log_fd -> |
63 | | - let pid = |
64 | | - safe_close_and_exec None (Some log_fd) (Some log_fd) |
65 | | - [(s_uuid, s)] |
66 | | - xen_bugtool params |
67 | | - in |
68 | | - waitpid_fail_if_bad_exit pid |
69 | | - ) |
| 64 | + |
| 65 | + let cmd_capabilities = Printf.sprintf "%s --capabilities" path |
| 66 | + |
| 67 | + let params_fd ~entries ~extension ~uuid = |
| 68 | + let params = |
| 69 | + params_cp ~entries ~extension @ [Printf.sprintf "--outfd=%s" uuid] |
70 | 70 | in |
71 | | - match result with |
72 | | - | Success _ -> |
73 | | - debug "xen-bugtool exited successfully" |
74 | | - | Failure (log, exn) -> |
75 | | - debug "xen-bugtool failed with output: %s" log ; |
76 | | - raise exn |
77 | | - with e -> |
78 | | - let msg = "xen-bugtool failed: " ^ Printexc.to_string e in |
79 | | - error "%s" msg ; |
80 | | - raise |
81 | | - (Api_errors.Server_error (Api_errors.system_status_retrieval_failed, [msg]) |
82 | | - ) |
| 71 | + let cmd = String.concat " " (path :: params) in |
| 72 | + L.debug "%s: running %s" __FUNCTION__ cmd ; |
| 73 | + params |
| 74 | + |
| 75 | + let cmd_cp ~entries ~extension = |
| 76 | + let params = params_cp ~entries ~extension in |
| 77 | + let cmd = String.concat " " (path :: params) in |
| 78 | + L.debug "%s: running %s" __FUNCTION__ cmd ; |
| 79 | + cmd |
| 80 | + |
| 81 | + let filename __context extension = |
| 82 | + let timestamp = Ptime_clock.now () |> Ptime.to_rfc3339 ~tz_offset_s:0 in |
| 83 | + let self = Helpers.get_localhost ~__context in |
| 84 | + let hostname = Db.Host.get_hostname ~__context ~self in |
| 85 | + Printf.sprintf "system_status-%s-%s.%s" timestamp hostname extension |
| 86 | +end |
| 87 | + |
| 88 | +let get_capabilities () = Helpers.get_process_output Bugtool.cmd_capabilities |
| 89 | + |
| 90 | +(* This fn outputs xen-bugtool straight to the socket, only |
| 91 | + for tar output. It should work on embedded edition *) |
| 92 | +let send_via_fd __context s entries output = |
| 93 | + let uuid = Uuidx.to_string (Uuidx.make ()) in |
| 94 | + let extension = Output.to_extension output in |
| 95 | + let content_type = Output.to_mime output in |
| 96 | + let filename = Bugtool.filename __context extension in |
| 97 | + let params = Bugtool.params_fd ~entries ~extension ~uuid in |
| 98 | + let headers = |
| 99 | + Http.http_200_ok ~keep_alive:false ~version:"1.0" () |
| 100 | + @ [ |
| 101 | + Printf.sprintf "Server: %s" Xapi_version.xapi_user_agent |
| 102 | + ; Printf.sprintf "%s: %s" Http.Hdr.content_type content_type |
| 103 | + ; Printf.sprintf {|%s: attachment; filename="%s"|} |
| 104 | + Http.Hdr.content_disposition filename |
| 105 | + ] |
| 106 | + in |
| 107 | + Http_svr.headers s headers ; |
| 108 | + let result = |
| 109 | + Forkhelpers.with_logfile_fd "get-system-status" (fun log_fd -> |
| 110 | + let pid = |
| 111 | + Forkhelpers.safe_close_and_exec None (Some log_fd) (Some log_fd) |
| 112 | + [(uuid, s)] |
| 113 | + Bugtool.path params |
| 114 | + in |
| 115 | + Forkhelpers.waitpid_fail_if_bad_exit pid |
| 116 | + ) |
| 117 | + in |
| 118 | + match result with Success _ -> Ok () | Failure (log, exn) -> Error (log, exn) |
83 | 119 |
|
84 | 120 | (* This fn outputs xen-bugtool into a file and then write the |
85 | 121 | file out to the socket, to deal with zipped bugtool outputs |
86 | 122 | It will not work on embedded edition *) |
87 | 123 | let send_via_cp __context s entries output = |
88 | | - let cmd = |
89 | | - sprintf "%s --entries=%s --silent --yestoall --output=%s" xen_bugtool |
90 | | - entries output |
91 | | - in |
92 | | - let () = debug "running %s" cmd in |
| 124 | + let extension = Output.to_extension output in |
| 125 | + let content_type = Output.to_mime output in |
| 126 | + let cmd = Bugtool.cmd_cp ~entries ~extension in |
93 | 127 | try |
94 | | - let filename = String.rtrim (Helpers.get_process_output cmd) in |
| 128 | + let filepath = String.trim (Helpers.get_process_output cmd) in |
| 129 | + let filename = Bugtool.filename __context extension in |
95 | 130 | let hsts_time = !Xapi_globs.hsts_max_age in |
96 | 131 | finally |
97 | 132 | (fun () -> |
98 | | - debug "bugball path: %s" filename ; |
99 | | - Http_svr.response_file ~mime_content_type:content_type ~hsts_time s |
100 | | - filename |
| 133 | + Http_svr.response_file ~mime_content_type:content_type ~hsts_time |
| 134 | + ~download_name:filename s filepath |
101 | 135 | ) |
102 | 136 | (fun () -> |
103 | 137 | Helpers.log_exn_continue "deleting xen-bugtool output" Unix.unlink |
104 | | - filename |
105 | | - ) |
106 | | - with e -> |
107 | | - let msg = "xen-bugtool failed: " ^ ExnHelper.string_of_exn e in |
108 | | - error "%s" msg ; |
109 | | - raise |
110 | | - (Api_errors.Server_error (Api_errors.system_status_retrieval_failed, [msg]) |
111 | | - ) |
| 138 | + filepath |
| 139 | + ) ; |
| 140 | + Ok () |
| 141 | + with e -> Error ("(Not captured)", e) |
| 142 | + |
| 143 | +let with_api_errors f ctx s entries output = |
| 144 | + match f ctx s entries output with |
| 145 | + | Ok () -> |
| 146 | + () |
| 147 | + | Error (log, exn) -> |
| 148 | + L.debug "xen-bugtool failed with output: %s" log ; |
| 149 | + let msg = "xen-bugtool failed: " ^ Printexc.to_string exn in |
| 150 | + raise Api_errors.(Server_error (system_status_retrieval_failed, [msg])) |
| 151 | + |
| 152 | +let send_capabilities req s = |
| 153 | + let content = get_capabilities () in |
| 154 | + let xml_type = "application/xml" in |
| 155 | + let hdrs = |
| 156 | + [ |
| 157 | + ("Server", Xapi_version.xapi_user_agent); (Http.Hdr.content_type, xml_type) |
| 158 | + ] |
| 159 | + in |
| 160 | + Http_svr.response_str req ~hdrs s content |
112 | 161 |
|
113 | 162 | let handler (req : Request.t) s _ = |
114 | | - debug "In system status http handler..." ; |
115 | 163 | req.Request.close <- true ; |
116 | | - let get_param s = try List.assoc s req.Request.query with _ -> "" in |
117 | | - let entries = get_param "entries" in |
118 | | - let output = get_param "output" in |
119 | | - let () = debug "session_id: %s" (get_param "session_id") in |
120 | | - Xapi_http.with_context task_label req s (fun __context -> |
121 | | - if Helpers.on_oem ~__context && output <> "tar" then |
122 | | - raise |
123 | | - (Api_errors.Server_error |
124 | | - (Api_errors.system_status_must_use_tar_on_oem, []) |
125 | | - ) |
126 | | - else if output = "tar" then |
127 | | - send_via_fd __context s entries output |
128 | | - else |
129 | | - send_via_cp __context s entries output |
130 | | - ) |
| 164 | + let get_param s = List.assoc_opt s req.Request.query in |
| 165 | + let list_capabilies = Option.is_some (get_param "list") in |
| 166 | + let entries = Option.value ~default:"" (get_param "entries") in |
| 167 | + let output = Option.bind (get_param "output") Output.of_string in |
| 168 | + |
| 169 | + let send_list () = send_capabilities req s in |
| 170 | + let send_file () = |
| 171 | + Xapi_http.with_context task_label req s @@ fun __context -> |
| 172 | + match |
| 173 | + (Helpers.on_oem ~__context, Option.value ~default:Output.Tar output) |
| 174 | + with |
| 175 | + | _, (Output.Tar as output) -> |
| 176 | + with_api_errors send_via_fd __context s entries output |
| 177 | + | false, output -> |
| 178 | + with_api_errors send_via_cp __context s entries output |
| 179 | + | true, _ -> |
| 180 | + raise Api_errors.(Server_error (system_status_must_use_tar_on_oem, [])) |
| 181 | + in |
| 182 | + |
| 183 | + if list_capabilies then send_list () else send_file () |
0 commit comments