简体   繁体   中英

Code to count number of types of files on client in Unix

Hi I am new to shell scripting. my requirement is: There is one server & 3 clients. On each client error logs files are generated which are of 4 types.Say type 1 error , type 2 error like this type 4 error. I want to write a script which read all the 3 clients from server & provide me number of times 4 different type of error logs are genereted on each client. In short it should use ssh & grep command combination. I have written the demo script but it's not providing me the number of times different type of logs occured on clients.

#error[1]='Exception: An application error occurred during an address lookup request, please contact IT'
#error[2]='SocketTimeoutException: Read timed out'
#error[3]='Exception: The search has produced too many matches to be returned'
#error[4]='Exception: No matching address found'
error_1='exception 1'
error_2='exception 2'
function get_list_of_clients()
{
NUM_OF_CLIENTS=$(wc -l ${CLIENT_IP_LIST} | awk -F " " '{ print $1 }' )
echo $NUM_OF_CLIENTS
      if [ "${NUM_OF_CLIENTS}" -gt 0 ]
      then
            for ((row=2; row<=$NUM_OF_CLIENTS; row++))
            do
                  CLIENTS_IP=$(sed -n ${row}p ${CLIENT_IP_LIST}| awk -F " " '{print $3 }')
                  echo ${CLIENTS_IP}
 #                 get_number_of_errors
 #                 copy_count_errors
            echo ${$error_$row}
            done
      fi

}

function get_number_of_errors()
{
for((row_no=1; row_no<=4; row_no++))
do
{
/usr/bin/expect - <<- EndMark
spawn ssh root@${CLIENTS_IP} "grep $error[$row_no] var/error.log |wc -l" >> /tmp/${CLIENTS_IP}_error${row_no}.txt
match_max 50000
  expect {
  "*yes/no*" {
   send -- "yes\r"
   send -- "\r"
   exp_continue
}
  "*?assword:*" {
   send -- "${CLIENT_PASSWORD}\r"
   send -- "\r"
     }

}
expect eof
EndMark
}
done
}


function copy_count_errors()
{
/usr/bin/expect - <<- EndMark
spawn scp root@${CLIENTS_IP}:/tmp/${CLIENTS_IP}* /tmp/
match_max 50000
  expect {
  "*yes/no*" {
   send -- "yes\r"
   send -- "\r"
   exp_continue
}
  "*?assword:*" {
   send -- "${CLIENT_PASSWORD}\r"
   send -- "\r"
  }
}
expect eof
EndMark
}

get_list_of_clients

================================================================================ please help.

This is not really an answer, an attempt to help you getting your own.

The Problem

If I understand it correctly:

  1. Your script runs on the server
  2. You have three clients, each has log files
  3. Your list of clients is in a file named $CLIENT_IP_LIST , where the IP is the third field and the first line is some sort of header, which you want to exclude.

Suggestions

  • It seems you need to ssh and scp to the clients. If possible, I suggest setting up SSH Public Key Authentication between your server and clients. This will greatly simplify your scripts.
  • Use the -C flag for compression with the scp and ssh commands.
  • You should copy the log files from the clients to the server and do processing on the server.
  • If you can choose a different scripting language such as Python , Tcl (You used Expect , which is really Tcl ). Bash can handle your problem, but you will find other languages work better. This is my opinion, of course.
  • Get one piece of your puzzle to work before moving on to the next. For example, right now, your get_list_of_clients() function does not yet working.

That being said, here is a rewrite of get_list_of_clients , which I tested and it works within my assumptions about your $CLIENT_IP_LIST file:

function get_list_of_clients() {
    let row=1
    while read line
    do
        if (( row > 1 ))
        then
            set $line # Breaks line into pieces
            CLIENT_IP="$3"
            echo "$CLIENT_IP"
        fi
        let row=row+1
    done < $CLIENT_IP_LIST
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM