<commit_before>(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *)

open Network_utils

open Fun
open Stringext
open Listext
open Threadext

module D = Debug.Debugger(struct let name = "network_monitor_thread" end)
open D

(** Table for bonds status. *)
let bonds_status : (string, (int * int)) Hashtbl.t = Hashtbl.create 10

let xapi_rpc request =
	Rpc_client.do_rpc_unix
		~content_type:(Rpc_client.content_type_of_string "text/xml")
		~filename:(Filename.concat "/var/lib/xcp" "xapi")
		~path:"/" request

let send_bond_change_alert dev interfaces message =
	let ifaces = String.concat "+" (List.sort String.compare interfaces) in
	let module XenAPI = Client.Client in
	let session_id = XenAPI.Session.login_with_password
		~rpc:xapi_rpc ~uname:"" ~pwd:"" ~version:"1.4" in
	Pervasiveext.finally
		(fun _ ->
			let obj_uuid = Inventory.lookup Inventory._installation_uuid in
			let body = Printf.sprintf	"The status of the %s bond %s" ifaces message in
			try
				let (name, priority) = ("bond_status_changed",3L) (* Api_messages.bond_status_changed *) in
				let (_ : API.ref_message) = XenAPI.Message.create ~rpc:xapi_rpc ~session_id
					~name ~priority ~cls:`Host ~obj_uuid ~body in ()
			with _ ->
				warn "Exception sending a bond-status-change alert."
		)
		(fun _ -> XenAPI.Session.logout ~rpc:xapi_rpc ~session_id)

let check_for_changes ~(dev : string) ~(stat : Network_monitor.iface_stats) =
	let open Network_monitor in
	match String.startswith "vif" dev with true -> () | false ->
	if stat.nb_links > 1 then ( (* It is a bond. *)
		if Hashtbl.mem bonds_status dev then ( (* Seen before. *)
			let nb_links_old, links_up_old = Hashtbl.find bonds_status dev in
			if links_up_old <> stat.links_up then (
				info "Bonds status changed: %s nb_links %d up %d up_old %d" dev stat.nb_links
				stat.links_up links_up_old;
				Hashtbl.replace bonds_status dev (stat.nb_links,stat.links_up);
				let msg = Printf.sprintf "changed: %d/%d up (was %d/%d)" stat.links_up stat.nb_links
					links_up_old nb_links_old in<commit_msg>Use Api_messages to get message priority<commit_after>(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *)

open Network_utils

open Fun
open Stringext
open Listext
open Threadext

module D = Debug.Debugger(struct let name = "network_monitor_thread" end)
open D

(** Table for bonds status. *)
let bonds_status : (string, (int * int)) Hashtbl.t = Hashtbl.create 10

let xapi_rpc request =
	Rpc_client.do_rpc_unix
		~content_type:(Rpc_client.content_type_of_string "text/xml")
		~filename:(Filename.concat "/var/lib/xcp" "xapi")
		~path:"/" request

let send_bond_change_alert dev interfaces message =
	let ifaces = String.concat "+" (List.sort String.compare interfaces) in
	let module XenAPI = Client.Client in
	let session_id = XenAPI.Session.login_with_password
		~rpc:xapi_rpc ~uname:"" ~pwd:"" ~version:"1.4" in
	Pervasiveext.finally
		(fun _ ->
			let obj_uuid = Inventory.lookup Inventory._installation_uuid in
			let body = Printf.sprintf	"The status of the %s bond %s" ifaces message in
			try
				let (name, priority) = Api_messages.bond_status_changed in
				let (_ : API.ref_message) = XenAPI.Message.create ~rpc:xapi_rpc ~session_id
					~name ~priority ~cls:`Host ~obj_uuid ~body in ()
			with _ ->
				warn "Exception sending a bond-status-change alert."
		)
		(fun _ -> XenAPI.Session.logout ~rpc:xapi_rpc ~session_id)

let check_for_changes ~(dev : string) ~(stat : Network_monitor.iface_stats) =
	let open Network_monitor in
	match String.startswith "vif" dev with true -> () | false ->
	if stat.nb_links > 1 then ( (* It is a bond. *)
		if Hashtbl.mem bonds_status dev then ( (* Seen before. *)
			let nb_links_old, links_up_old = Hashtbl.find bonds_status dev in
			if links_up_old <> stat.links_up then (
				info "Bonds status changed: %s nb_links %d up %d up_old %d" dev stat.nb_links
				stat.links_up links_up_old;
				Hashtbl.replace bonds_status dev (stat.nb_links,stat.links_up);
				let msg = Printf.sprintf "changed: %d/%d up (was %d/%d)" stat.links_up stat.nb_links
					links_up_old nb_links_old in
{'fraction_non_alphanumeric': 0.0954153605015674, 'fraction_numerical': 0.002938871473354232, 'mean_word_length': 4.120361083249749, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3084942', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1646, 'n_words': 559}