通过NSH获取被管服务器属性信息

通过该方法可以把服务器配置信息输出为CSV文件.

注:脚本可执行的前提是你已经配置了blcli环境.

 

 

1. 创建服务器列表文件

 

# cat servers.txt
server1
server2
server3
server4
server5
 

 

2. 创建配置属性文件

 

# cat properties
ASSET_ID
ASSET_TAG
CABINET
WARRANTY_EXPIRATION
REMOTE_ACCESS_NAME
ENVIRONMENT
DR_REQUIREMENTS
PATCH_GROUP
SITE
ASSET_MANAGER
AIT_SUPPORT_ROLE
OWNER
DIVISIONAL_CONTACT
DEPARTMENT
MAINTENANCE_WINDOW
ITEM
 

 

3. 创建脚本文件

 

#!/usr/nsh/bin/nsh
 
if [ 2 -ne $# ]
then
  echo "usage: $0 full_path_to_list_of_servers full_path_to_list_of_properties"
  exit
fi
 
if [ ! -e "$1" ] || [ ! -e "$2" ]
then
  echo "can't find either $1 or $2"
  exit
fi
 
SERVERFILE="$1"
TAGSFILE="$2"
date >&2
echo "using $SERVERFILE for the list of servers" >&2
echo "using $TAGSFILE for the list of properties" >&2
 
VALIDTAGS=""
validate_tags () {
  blcli_execute PropertySetClass getAllPropertyNames Class://SystemObject/Server > /dev/null 2>&1
  blcli_storeenv thetags
  for i in `cat "$TAGSFILE" |tr -d '\015'| sort`
  do
    echo "$thetags" | grep $i >/dev/null 2>&1
    if [ 0 -eq $? ]
    then
      VALIDTAGS=`echo "$VALIDTAGS""|""^$i"`
    fi
  done
  VALIDTAGS=`echo "$VALIDTAGS" | sed -e 's/^|\(.*\)/\1/'`
}
 
echo "connecting" >&2
blcli_connect
if [ $? -ne 0 ]
then
  echo "can't connect blcli"
  exit
fi
echo "connected." >&2
 
echo "validating the list of properties" >&2
validate_tags
 
if [ -z "$VALIDTAGS" ]
then
  echo "no parameter is valid"
  exit
else
  echo "the following properties are valid:"`echo "$VALIDTAGS" | tr '|' ','` | tr -d ^ >&2
fi
 
 
echo "processing list of servers" >&2
echo "server,""`echo \"$VALIDTAGS\" | tr '|' ',' | tr -d ^`"
for server in `cat $SERVERFILE | tr -d '\015'`
do
  blcli_execute Server printAllProperties $server > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    blcli_storeenv theinstance
    echo -n "$server"","
    echo "$theinstance" | grep -E "$VALIDTAGS"  | sort | cut -d '=' -f 2 | awk 'BEGIN {ORS=",";} {print $0}' | sed -e 's/\(.*\),$/\1/'
    echo ""
  fi
done
blcli_disconnect
 
date >&2
echo "done" >&2

 

 

4. 脚本执行及输出

 

# nsh get_properties_for_servers.nsh 5servers.txt properties > sample_output.csv
Wed Oct 8 05:33:39 EDT 2008
using servers.txt for the list of servers
using properties for the list of properties
connecting
connected.
validating the list of properties
the following properties are valid:ASSET_ID,ENVIRONMENT,MAINTENANCE_WINDOW,PATCH_GROUP
processing list of servers
Wed Oct 8 05:33:47 EDT 2008
done
 
suprhas30serv1# cat sample_output.csv
server,ASSET_ID,ENVIRONMENT,MAINTENANCE_WINDOW,PATCH_GROUP
server1, AST0047093, Test Lab, Sunday 12:00 a.m. - 4:00 a.m., EUT - TEST
server2, AST0047107, Test Lab, Sunday 12:00 a.m. - 4:00 a.m., EUT - TEST
server3, AST0047120, Test Lab, Sunday 12:00 a.m. - 4:00 a.m., EUT - TEST
server4, AST0047177, Test Lab, Sunday 12:00 a.m. - 4:00 a.m., EUT - TEST
server5, AST0047122, Test Lab, Sunday 12:00 a.m. - 4:00 a.m., EUT - TEST

你可能感兴趣的:(服务器)