Загрузка данных


#!/usr/bin/env bash

set -u

if [[ $# -ne 4 ]]; then
    echo "Usage: $0 <input_file> <agent_address> <existing_oids_file> <removed_oids_file>"
    exit 1
fi

INPUT_FILE="$1"
AGENT="$2"
EXISTING_FILE="$3"
REMOVED_FILE="$4"

COMMUNITY="public"

kept=0
succeeded=0
no_instance=0
removed=0

> "$EXISTING_FILE"
> "$REMOVED_FILE"

while IFS= read -r oid || [[ -n "$oid" ]]; do
    [[ -z "$oid" ]] && continue

    result=$(snmpget -v2c -c "$COMMUNITY" "$AGENT" "$oid" 2>&1)

    if grep -qi "No Such Object" <<< "$result"; then
        echo "REMOVE:  $oid"

        # Keep the complete snmpget output
        echo "$result" >> "$REMOVED_FILE"

        ((removed++))

    elif grep -qi "No Such Instance" <<< "$result"; then
        echo "KEEP:    $oid (object exists, instance absent)"

        # Keep the complete snmpget output
        echo "$result" >> "$EXISTING_FILE"

        ((kept++))
        ((no_instance++))

    else
        echo "SUCCESS: $oid"

        # Keep the complete successful snmpget output
        echo "$result" >> "$EXISTING_FILE"

        ((kept++))
        ((succeeded++))
    fi
done < "$INPUT_FILE"

echo
echo "Summary:"
echo "  Kept:                 $kept"
echo "    Successful:         $succeeded"
echo "    Missing instances:  $no_instance"
echo "  Removed (no object):  $removed"
echo "  Existing OIDs file:   $EXISTING_FILE"
echo "  Removed OIDs file:    $REMOVED_FILE"