#!/bin/bash

# Step 1
rpm_version=$(/bin/cat /etc/popagent/RPM_version | awk -F[.] '{print $1$2$3}')
if [ $(awk 'BEGIN{print ("'"$rpm_version"'" > "551")}') -eq 1 ]; then
    pwd="lw-conf"
else
    pwd="lightwan"
fi

# Step 2
policy_table_output=$(cat /proc/net/$pwd/policy_table | grep zone=9 | grep drop)
if [ -n "$policy_table_output" ]; then
    # Step 3
    ipsetid=$(echo "$policy_table_output" | grep -oP "ipsetid:\K\d+")
    acsnameid=$(echo "$policy_table_output" | grep -oP "acsnameid:\K\d+")
    aces=$(cat /proc/net/$pwd/acsname | grep $ipsetid | grep $acsnameid | awk '{print $2}')
    count=$(cat /proc/net/$pwd/acsname-index/$aces | wc -l)
else
    # Step 4
    count="null"
    policy_table_output="null"
fi

# Step 5, 6, 7
POPRegion=$(grep POPRegion /etc/motd | awk '{print $4}')
OrchAddr=$(grep OrchAddr /etc/motd | awk '{print $4}')
POPName=$(grep POPName /etc/motd | awk '{print $4}')

# Step 8
#echo "POPRegion,OrchAddr,POPName,cat_result,count" > ~/output.csv
echo "$POPRegion,$OrchAddr,$POPName,$(/bin/cat /etc/popagent/RPM_version),$count,$policy_table_output" > /home/output.csv
