
{
    "name"  : "HG类异常",
    "problems" : {
        "hg_master" : {
            "desc"      : "【问题】${0}的HG口存在收发异常。",
            "advice"    : "【建议】建议联系后台处理。"
        },
        "hg_backup" : {
            "desc"      : "【问题】SLOT ${0}的HG口存在收发异常。",
            "advice"    : "【建议】建议联系后台处理。"
        }
    }
}

---------------------------code-------------------------------
result = 'pass'
port = ''
log_slot = cli('show version slot')
log_slotList = log_slot.split("\r\n") 
reg = /(\d+)\s+(\d+)\s+\d+\s+[^ ]+\s+[^ ]+\s+ok/
foreach data in log_slotList:
	r = data.match(reg)
	if r != null:
		dev_id = r[1]
		slot_id = r[2]
		log_hg = getHgLog(slot_id)
		hg_count = getCount(log_hg)
		while hg_count >= 0:
			StepResult = isFail(log_hg,hg_count)
			if StepResult == 'fail':
				hg_num = hg_count
				result = 'fail'
                port = port + 'Slot '+ dev_id + '/' + slot_id + ' hg' + hg_num + '、'
			end
			hg_count = hg_count - 1
		end
	end
end

if result == 'fail':
    change_title('HG口存在异常转发')
    add_problem('hg_master',[port])
    return 'warn'
end

change_title('HG口无转发异常')
return 'success'

def isFail(str,num):
	result = 'pass'
	t_count = 0
	r_count = 0
	strList = str.split("\r\n")
	reg_t = format('TPKT.hg%s\\s+:\\s+[^ ]+\\s+[^ ]+\\s+([^ ]+)\/s',num)
	#reg_t = /TPKT.hg3\s+:\s+[^ ]+\s+[^ ]+\s+([^ ]+)\/s/
			#TPKT.hg3undefined+:undefined+[^ ]+undefined+[^ ]+undefined+([^ ]+)/s
	reg_r = format('RPKT.hg%s\\s+:\\s+[^ ]+\\s+[^ ]+\\s+([^ ]+)\/s',num)
	#reg_r = /RPKT.hg3\s+:\s+[^ ]+\s+[^ ]+\s+([^ ]+)\/s/
	foreach data in strList:
		r_t = data.match(reg_t)
		r_r = data.match(reg_r)
		if r_t != null:
			t_countStr = r_t[1]
			t_count = str_to_num(t_countStr)
		end
		if r_r != null:
			r_countStr = r_r[1]
			r_count = str_to_num(r_countStr)
		end
	end
	if t_count != 0 && r_count != 0:
		value = t_count/r_count
		print(value)
		if value>1.1 || value < 0.9 :
			result = 'fail'
		end
	end
	return result
end

def str_to_num(str):
	num = ''
	strList = str.split(",")
	foreach data in strList:
		num = num+data
	end
	return num
end

def getCount(str):
	count = 1
	if str.indexOf('hg4'):
		count = 4
	else if str.indexOf('hg3'):
		count = 3
	else if str.indexOf('hg2'):
		count = 2
	end
	return count
end

def getHgLog(slot_id):
	log_hg = ''
	cli('run-system-shell')
	cmd = 'debug-ssa ' + slot_id
	cli(cmd,'>')
	log_hg = cli('show c hg','>')
	cli('@ctrl+]','e exit telnet')
	cli('e')
	cli('exit')
	return log_hg
end

