Skip to content

Commit

Permalink
Add fake SNMP reset tool
Browse files Browse the repository at this point in the history
The flush test sends an SNMP request to PDU to reset the
power on the hardware PC. As the flush test sends only one
type of request we don't need to parse it and can just wait
for any UDP packet and perform a hard reset of the VM.

Signed-off-by: Kostiantyn Kostiuk <[email protected]>
  • Loading branch information
kostyanf14 committed Oct 3, 2024
1 parent 963fa60 commit 54d73fa
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions bin/fake-snmp-reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Intended to be usable as a standalone script
# it is not a part of the AutoHCK module

require 'json'
require 'logger'
require 'socket'

class SnmpReset
def initialize(logger, socket_file)
@logger = logger

@socket_file = socket_file

@qmp_connected = false
end

def connect_socket
@logger.info "Initializing QMP session for #{@socket_file}"
@qmp_socket = UNIXSocket.new(@socket_file)
end

def run
@logger.info 'Initializing UDP socket'
UDPSocket.open do |socket|
socket.bind('0.0.0.0', 'snmp')
@logger.info "Listening on #{socket.local_address.inspect_sockaddr}"

count = 0

loop do
_, addr = socket.recvfrom(512)

@logger.info "Received data from #{addr[3]}:#{addr[1]}"

unless @qmp_connected
connect_socket
send_cmd 'qmp_capabilities'
@qmp_connected = true
end

count += 1
send_cmd 'system_reset'
@logger.info "Rebooting... #{count}"
end
end
end

private

def send_cmd(cmd)
@logger.info "Sending #{cmd} via QMP"
@qmp_socket.write JSON.dump({ 'execute' => cmd })
@qmp_socket.flush

loop do
response = JSON.parse(@qmp_socket.readline)
break response['return'] if response.key?('return')
raise response['error'].to_s if response.key?('error')
end
end
end

log = Logger.new('fake-snmp-reset.txt')
log.level = Logger::DEBUG
log.info "START #{ARGV}"

snmp = SnmpReset.new(log, ARGV[0])
snmp.run

0 comments on commit 54d73fa

Please sign in to comment.