diff --git a/src/mad/ruby/scripts_common.rb b/src/mad/ruby/scripts_common.rb index 944b7a3567..f77ff795c3 100644 --- a/src/mad/ruby/scripts_common.rb +++ b/src/mad/ruby/scripts_common.rb @@ -14,6 +14,11 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'augeas' +require 'base64' +require 'zlib' +require 'socket' + module OpenNebula # Generic log function @@ -92,4 +97,61 @@ module OpenNebula log "Executed \"#{command}\"." end + def self.send_to_monitor(msg_type, result, oid, data) + # Read monitord.conf + one_location = ENV['ONE_LOCATION'] + + if !one_location + file_dir = '/etc/one/' + else + file_dir = one_location + '/etc/' + end + + file_name = 'monitord.conf' + + aug = Augeas.create(:no_modl_autoload => true, + :no_load => true, + :root => file_dir, + :loadpath => file_name) + + aug.clear_transforms + aug.transform(:lens => 'Oned.lns', :incl => file_name) + aug.context = "/files/#{file_name}" + aug.load + + mon_address = aug.get('NETWORK/MONITOR_ADDRESS') + mon_port = aug.get('NETWORK/PORT') + mon_key = aug.get('NETWORK/PUBKEY').tr('"', '') + + mon_address = "127.0.0.1" if mon_address.include? "auto" + + # Encrypt + if mon_key && !mon_key.empty? + block_size = mon_key.n.num_bytes - 11 + + edata = '' + index = 0 + + loop do + break if index >= data.length + + edata << mon_key.public_encrypt(data[index, block_size]) + + index += block_size + end + + data = edata + end + + # Send data + zdata = Zlib::Deflate.deflate(data, Zlib::BEST_COMPRESSION) + data64 = Base64.strict_encode64(zdata) + + result = "SUCCESS" if result == "0" || result == 0 + + msg = "#{msg_type} #{result} #{oid} #{data64}" + + socket_udp = UDPSocket.new() + socket_udp.send(msg, 0, mon_address, mon_port) + end end diff --git a/src/mad/sh/scripts_common.sh b/src/mad/sh/scripts_common.sh index 3255461c50..ec879023c5 100644 --- a/src/mad/sh/scripts_common.sh +++ b/src/mad/sh/scripts_common.sh @@ -1039,3 +1039,50 @@ function get_nic_information { OUTBOUND_PEAK_KB="${XPATH_ELEMENTS[j++]}" ORDER="${XPATH_ELEMENTS[j++]}" } + +# Send message to monitor daemon +# This function reads monitor network and encryption settings from monitord.conf, +# packs and optionally encrypt the message and sends it to monitor daemon +# Parameters +# $1 Message type: MONITOR_VM, BEACON_HOST, MONITOR_HOST, SYSTEM_HOST, STATE_VM, LOG, ... +# $2 Result of the operation: +# 0 or "SUCCESS" means succesful monitoring +# everything else is failure +# $3 Object ID, use -1 if not relevant +# $4 Message payload (the monitoring data) +function send_to_monitor { + msg_type="$1" + msg_result="$2" + msg_oid="$3" + msg_payload="$4" + + if [ "$msg_result" = "0" ]; then + msg_result=SUCCESS + fi + + # Read monitord config + if [ -z "${ONE_LOCATION}" ]; then + mon_conf=/etc/one/monitord.conf + else + mon_conf=$ONE_LOCATION/etc/monitord.conf + fi + + mon_config=$(augtool -L -l $mon_conf ls /files/$mon_conf/NETWORK) + mon_address=$(echo "$mon_config" | grep MONITOR_ADDRESS | awk '{print $3}') + mon_port=$(echo "$mon_config" | grep PORT | awk '{print $3}') + mon_key=$(echo "$mon_config" | grep PUBKEY | awk '{print $3}') + + if [[ $mon_address == *"auto"* ]]; then + mon_address="127.0.0.1" + fi + + # Send message + # todo if mon_key is not empty do message encryption + + payload_b64="$(echo $msg_payload | \ + ruby -e "require 'zlib'; puts Zlib::Deflate.deflate(STDIN.read)" | \ + base64 -w 0)" + + echo "$msg_type $msg_result $msg_oid $payload_b64" | + nc -u -w1 $mon_address $mon_port +} \ No newline at end of file diff --git a/src/monitor/src/monitor/MonitorDriverProtocol.cc b/src/monitor/src/monitor/MonitorDriverProtocol.cc index 57ab93dd83..001cc4d755 100644 --- a/src/monitor/src/monitor/MonitorDriverProtocol.cc +++ b/src/monitor/src/monitor/MonitorDriverProtocol.cc @@ -155,7 +155,10 @@ void MonitorDriverProtocol::_beacon_host(message_t msg) NebulaLog::warn("MDP", "Error condition detected on beacon for host " + to_string(msg->oid()) + ": " + msg->payload()); - hm->error_monitor(msg->oid(), msg->payload()); + if (msg->oid() >= 0) + { + hm->error_monitor(msg->oid(), msg->payload()); + } return; } diff --git a/src/tm_mad/shared/monitor b/src/tm_mad/shared/monitor index b689f6b0de..53fc5558ff 100755 --- a/src/tm_mad/shared/monitor +++ b/src/tm_mad/shared/monitor @@ -75,49 +75,48 @@ echo "USED_MB=\$USED_MB" echo "FREE_MB=\$FREE_MB" echo "TOTAL_MB=\$TOTAL_MB" -if [ $MONITOR_VM_DISKS -eq 1 ]; then - vms=\$(ls "$BASE_PATH" | grep '^[0-9]\+$') +EOF +) - monitor="" +MONITOR_VM_SCRIPT=$(cat </dev/null | grep '^disk\.[0-9]\+$') +vms=\$(ls "$BASE_PATH" | grep '^[0-9]\+$') - [ -z \$disks ] && continue +monitor="" - vm_monitor="" +for vm in \$vms; do + vmdir="${BASE_PATH}/\${vm}" + disks=\$(ls "\$vmdir" 2>/dev/null | grep '^disk\.[0-9]\+$') - for disk in \$disks; do - disk_id="\$(echo "\$disk" | cut -d. -f2)" - disk_size="\$(du -mL "\${vmdir}/\${disk}" 2>/dev/null | awk '{print \$1}')" - snap_dir="\${vmdir}/\${disk}.snap" + [ -z \$disks ] && continue - [ -z "\$disk_size" ] && continue + vm_monitor="" - vm_monitor="\${vm_monitor} DISK_SIZE = [ ID=\${disk_id}, SIZE=\${disk_size}]" + for disk in \$disks; do + disk_id="\$(echo "\$disk" | cut -d. -f2)" + disk_size="\$(du -mL "\${vmdir}/\${disk}" 2>/dev/null | awk '{print \$1}')" + snap_dir="\${vmdir}/\${disk}.snap" - if [ -e "\$snap_dir" ]; then - snaps="\$(ls "\$snap_dir" 2>/dev/null | grep '^[0-9]$')" + [ -z "\$disk_size" ] && continue - for snap in \$snaps; do - snap_size="\$(du -mL "\${snap_dir}/\${snap}" 2>/dev/null | awk '{print \$1}')" - [ -z "\$snap_size" ] && continue + vm_monitor="\${vm_monitor} DISK_SIZE = [ ID=\${disk_id}, SIZE=\${disk_size}]" - vm_monitor="\${vm_monitor}\nSNAPSHOT_SIZE = "\ - "[ ID=\${snap}, DISK_ID=\${disk_id}, SIZE=\${snap_size}]" - done - fi - done + if [ -e "\$snap_dir" ]; then + snaps="\$(ls "\$snap_dir" 2>/dev/null | grep '^[0-9]$')" - vm_monitor_b64="\$(echo \$vm_monitor | base64 -w 0)" - monitor="\${monitor}VM=[ID=\$vm,MONITOR=\"\$vm_monitor_b64\"] " + for snap in \$snaps; do + snap_size="\$(du -mL "\${snap_dir}/\${snap}" 2>/dev/null | awk '{print \$1}')" + [ -z "\$snap_size" ] && continue + + vm_monitor="\${vm_monitor}\nSNAPSHOT_SIZE = "\ + "[ ID=\${snap}, DISK_ID=\${disk_id}, SIZE=\${snap_size}]" + done + fi done - monitor_b64="\$(echo \$monitor | ruby -e "require 'zlib';\ - puts Zlib::Deflate.deflate(STDIN.read)" | base64 -w 0)" - echo MONITOR_VM SUCCESS 0 \$monitor_b64 | nc -u -w0 127.0.0.1 4124 -fi + vm_monitor_b64="\$(echo \$vm_monitor | base64 -w 0)" + echo "VM=[ID=\$vm, MONITOR=\"\$vm_monitor_b64\"] " +done EOF ) @@ -125,16 +124,26 @@ EOF if [ -n "$BRIDGE_LIST" ]; then HOST=`get_destination_host` MONITOR_DATA=$(ssh_monitor_and_log "$HOST" "$MONITOR_SCRIPT" "Remote monitor script" 2>&1) + MONITOR_STATUS=$? + + if [ $MONITOR_VM_DISKS -eq 1 ]; then + MONITOR_DATA_VMS=$(ssh_monitor_and_log "$HOST" "$MONITOR_VM_SCRIPT" "Remote VM disks monitor script" 2>&1) + MONITOR_VMS_STATUS=$? + fi else MONITOR_DATA=$(monitor_and_log "$MONITOR_SCRIPT" "Monitor script" 2>&1) + MONITOR_STATUS=$? + + if [ $MONITOR_VM_DISKS -eq 1 ]; then + MONITOR_DATA_VMS=$(monitor_and_log "$MONITOR_VM_SCRIPT" "VM disks monitor script" 2>&1) + MONITOR_VMS_STATUS=$? + fi fi -MONITOR_STATUS=$? +echo $MONITOR_DATA -if [ "$MONITOR_STATUS" = "0" ]; then - echo "$MONITOR_DATA" - exit 0 -else - echo "$MONITOR_DATA" - exit $MONITOR_STATUS +if [ $MONITOR_VM_DISKS -eq 1 ]; then + send_to_monitor MONITOR_VM $MONITOR_VMS_STATUS -1 "$MONITOR_DATA_VMS" fi + +exit $MONITOR_STATUS