-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_functions.h
1786 lines (1519 loc) · 49.1 KB
/
common_functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#############################################
# Purpose: These are common functions that may
# be included in other scripts with one of the
# following:
# . common_functions.h
# source common_functions.h
# Author: ???
# Revision: $Rev$
# Updated by: $Author$
# Last change date: $LastChangedDate$
# SVN URL: $HeadURL$
#############################################
# Some of these functions are dependent upon a timestamp variable called TS
# In general the script using the function should provide this, but in case it does not
# One will be generated here
if [[ -z $TS ]]; then
export TS=`date +%Y%m%d%H%M%S`
fi
#-----------------
# Function: f_CreateBondMaster
#-----------------
# Accepts the IP, Netmask and filename
# and creates a bond config
#-----------------
# Usage: f_CreateBondMaster <IP> <DEVNAME> <NETMASK> <FILE>
#-----------------
# Returns: SUCCESS, FAILURE
f_CreateBondMaster () {
IP=$1
DEV=$2
NM=$3
FN=$4
RESULT=SUCCESS
# If the config file already exists, back it up
if [[ -f $FN ]]; then
#mv $FN ${FN}.${TS}
if [[ $? -ne 0 ]]; then
RESULT=FAILURE
fi
fi
if [[ $RESULT != FAILURE ]]; then
# Write out the File
cat << EOF > $FN
DEVICE=${DEV}
ONBOOT=yes
IPADDR=${IP}
NETMASK=${NM}
GATEWAYDEV=${DEV}
PEERDNS=no
USERCTL=no
EOF
fi
if [[ -z `grep $DEV $FN` ]] || [[ -z `grep $IP $FN` ]] || [[ -z `grep $NM $FN` ]]; then
RESULT=FAILURE
if [[ -f ${FN}.${TS} ]]; then
#If the new bond script fails validation, restore the backup if there is one
/bin/cp ${FN}.${TS} ${FN}
fi
fi
echo $RESULT
}
#-----------------
# Function: f_DetectVM
#-----------------
# Determines if the server is a VM
#-----------------
# Usage: f_DetectVM
#-----------------
# Returns: TRUE | FALSE
f_DetectVM () {
RESULT=FALSE
if [[ -n `/usr/sbin/dmidecode | awk /"System Information"/,/"Serial Number"/ | grep "Product" | egrep -i 'rhev|vmware'` ]]; then
RESULT=TRUE
fi
echo $RESULT
}
#-----------------
# Function: f_FindPubIP
#-----------------
# Determines the public IP of the current server
#-----------------
# Usage: f_FindPubIP
#-----------------
# Returns: <IPv4 Address>, FAILURE
f_FindPubIP () {
#Method 1 - ifconfig - only really works if an IP is already assigned to a running device
#IPLIST=`ifconfig -a | grep "inet addr" | awk '{print $2}' | awk -F':' '{print $NF}' | grep -v 127.0.0.1`
IPLIST=`/sbin/ifconfig -a | grep "inet " | awk '{print $2}' | awk -F':' '{print $NF}' | grep -v 127.0.0.1`
if [[ -z $IPLIST ]]; then
#Method 2 - DNS/hosts file
HN=`hostname`
IPLIST=`getent hosts $HN`
fi
if [[ `echo $IPLIST | wc -w` -eq 1 ]]; then
# If we only have one IP then go with it
RESULT=$IPLIST
else
# If we have more than one IP then we need to figure out which
# is the public IP
# We'll do this by looking at the default gateway
GW=`f_FindDefaultGW`
if [[ $GW != FAILURE ]]; then
# Check to see if the first thee octets of an IP match
# the first three octets of the gateway - that should
# mean that's our public IP
FTO=`echo $GW | awk -F'.' '{print $1"."$2"."$3"."}'`
for i in $IPLIST; do
if [[ -n `echo $i | grep ^$FTO` ]]; then
RESULT=$i
fi
done
fi
fi
if [[ -z $RESULT ]] || [[ $RESULT == NOLINK ]]; then
RESULT=FAILURE
fi
echo $RESULT
}
#-----------------
# Function: f_FindDefaultGW
#-----------------
# Determines the default gateway of the machine
#-----------------
# Usage: f_FindDefaultGW
#-----------------
# Returns: <IPv4 Gateway Address>, FAILURE
f_FindDefaultGW () {
RESULT=FAILURE
# Method 1 - read from sysconfig
GW=`grep ^GATEWAY /etc/sysconfig/network | awk -F'=' '{print $NF}'`
# Method 2 - the gateway via route
if [[ -z $GW ]]; then
GW=`route -n | grep ^0.0.0.0 | awk '{print $2}'`
fi
if [[ -n $GW ]]; then
RESULT=$GW
fi
echo $RESULT
}
#-----------------
# Function: f_CreateBondSlave
#-----------------
# Accepts the Device Name, Bond Name, and File Name
# and creates a slave config
#-----------------
# Usage: f_CreateBondSlave <DEV> <MASTER> <FILE>
#-----------------
# Returns: SUCCESS, FAILURE
f_CreateBondSlave () {
DEV=$1
MASTER=$2
FN=$3
RESULT=SUCCESS
# If the config file already exists, try to read the HWADDR from it
if [[ -f $FN ]]; then
HWADDR=`grep "^HWADDR=" $FN | awk -F'=' '{print $2}'`
fi
# if bonding was set up previously, read the real hwaddr
if [[ -z $HWADDR ]]; then
if [[ -d /proc/net/bonding ]]; then
PROCBOND=`grep -R $DEV /proc/net/bonding/ | awk -F':' '{print $1}'`
if [[ -n $PROCBOND ]]; then
HWADDR=`cat $PROCBOND | awk "/${DEV}/,/Permanent/" | grep Permanent | awk '{print $NF}'`
fi
fi
fi
# if it's still not set, then fall back on the output of ifconfig
if [[ -z $HWADDR ]]; then
HWADDR=`/sbin/ifconfig ${DEV} | grep -i hwaddr | awk '{print $NF}'`
fi
if [[ $RESULT != FAILURE ]]; then
# Write out the File
cat << EOF > $FN
DEVICE=${DEV}
ONBOOT=yes
TYPE=Ethernet
BOOTPROTO=none
MASTER=${MASTER}
SLAVE=yes
USERCTL=no
PEERDNS=no
HWADDR=$HWADDR
#ETHTOOL_OPTS='speed 100 duplex full autoneg off'
#ETHTOOL_OPTS='autoneg on'
EOF
fi
if [[ -z `grep $DEV $FN` ]] || [[ -z `grep $MASTER $FN` ]]; then
RESULT=FAILURE
if [[ -f ${FN}.${TS} ]]; then
#If the new config failed then restore the backup if we made one
/bin/cp ${FN}.${TS} ${FN}
fi
fi
echo $RESULT
}
#-----------------
# Function: f_CheckIF
#-----------------
# Checks NIC device names to ensure they
# exist and are up
#-----------------
# Usage: f_CheckIF <DEV>
#-----------------
# Returns: SUCCESS, FAILURE, NOLINK
f_CheckIF () {
DEV=$1
RESULT=SUCCESS
if [[ -n `ethtool -i $DEV 2>&1 | grep "No such device"` ]]; then
RESULT=FAILURE
elif [[ -z `ethtool $DEV | grep Link | grep -i yes` ]]; then
RESULT=NOLINK
fi
echo $RESULT
}
#-----------------
# Function: f_IPforIF
#-----------------
# Checks for an IP assigned to a NIC
#-----------------
# Usage: f_IPforIF <DEV>
#-----------------
# Returns: <IP>, NONE
f_IPforIF () {
unset RESULT
IF=$1
if [[ -f /etc/sysconfig/network-scripts/ifcfg-${IF} ]]; then
RESULT=`grep ^IPADDR /etc/sysconfig/network-scripts/ifcfg-${IF} | awk -F'=' '{print $NF}'`
fi
if [[ -z $RESULT ]]; then
RESULT=`ifconfig $IF | grep "inet addr" | awk '{print $2}' | awk -F':' '{print $NF}'`
fi
if [[ -z $RESULT ]] || [[ $RESULT == NOLINK ]]; then
RESULT=NONE
fi
echo $RESULT
}
# Function: f_GetRelease
#-----------------
# Reads distro/release info and responds with three fields:
# The abbreviated Distro name, the Release version, the Update version
#-----------------
# Usage: f_GetRelease
#-----------------
# Returns: <DISTRO> <RELEASE> <UPDATE>
f_GetRelease () {
DISTRO=NON_REDHAT
RELEASE=0
UPDATE=0
if [[ -f /etc/redhat-release ]]; then
if [[ -z `cat /etc/redhat-release | grep Update` ]]; then
VERSION=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk '{print $1}'`
if [[ -z `echo $VERSION | grep '.'` ]]; then
RELEASE=$VERSION
UPDATE=0
else
RELEASE=`echo $VERSION | awk -F'.' '{print $1}'`
UPDATE=`echo $VERSION | awk -F'.' '{print $2}'`
fi
else
RELEASE=`cat /etc/redhat-release | awk -F'release' '{print $NF}' | awk '{print $1}'`
UPDATE=`cat /etc/redhat-release | tr -d '()' | awk -F'Update' '{print $NF}' | awk '{print $1}'`
VERSION=${RELEASE}.${UPDATE}
fi
if [[ -n `cat /etc/redhat-release | grep "Red Hat Enterprise Linux Server release"` ]]; then
DISTRO=RHEL
elif [[ -n `cat /etc/redhat-release | grep "Red Hat Enterprise Linux Everything release"` ]]; then
DISTRO=RHEL
elif [[ -n `cat /etc/redhat-release | grep "Red Hat Linux release"` ]]; then
DISTRO=RH
elif [[ -n `cat /etc/redhat-release | grep "Red Hat Enterprise Linux ES release"` ]]; then
DISTRO=RHES
elif [[ -n `cat /etc/redhat-release | grep "Red Hat Enterprise Linux AS release"` ]]; then
DISTRO=RHAS
else
DISTRO=UNKNOWN_REDHAT
fi
fi
echo "$DISTRO $RELEASE $UPDATE"
}
#-----------------
# Function: f_ValidIPv4
#-----------------
# Checks a string to ensure it contains a valid
# ipv4 address
#-----------------
# Usage: f_ValidIPv4 <IP>
#-----------------
# Returns: TRUE, FALSE
f_ValidIPv4 () {
IPv4=$1
# Valid until proven otherwise
RESULT=TRUE
# Does it have 4 octets?
if [[ `echo $IPv4 | awk -F'.' '{print NF}'` -ne 4 ]]; then
RESULT=FALSE
else
# Look at each octet
for o in `echo $IPv4 | sed 's/\./ /g'`; do
# Is the octet numeric?
if [[ -z `echo $o | egrep "^[0-9]+$"` ]]; then
RESULT=FALSE
else
# Is the octet less than 0 or greater than 255?
if [[ $o -lt 0 ]] || [[ $o -gt 255 ]]; then
RESULT=FALSE
fi
fi
done
fi
echo $RESULT
}
#-----------------
# Function: f_MakeSiteMenu
#-----------------
# Reads a text file with "<number>:<name>" format and displays a
# multi-column menu to the screen to ask for choices.
#-----------------
# Usage: f_MakeSiteMenu
#-----------------
# Returns: Menu | FAILURE
f_MakeSiteMenu() {
MENUFILE=$1
if [[ -s $MENUFILE ]]; then
# The number of spaces to indent the first menu column
LEFTINDENT=3
# The width of each menu column
#COLWIDTH=30
# Determine width dynamically
LONGEST=0
for s in `cat $MENUFILE | awk -F':' '{print $2}'`; do
if [[ `echo $s | wc -m` -gt $LONGEST ]]; then
LONGEST=`echo $s | wc -m`
fi
done
let COLWIDTH=$LONGEST+3
# The number of entries in each column
COLHEIGHT=10
# Start the row counter at 0
row=0
# Start the "character" column counter at the left indent
col=$LEFTINDENT
# Read in each line of the file
for ENTRY in `grep -v ^# $MENUFILE`; do
# Move cursor to the right according to the col variable
for (( i=1; i<=$col; i++ )); do
tput cuf1
done
# Sift out the number from the name
ENTNUM=`echo $ENTRY | awk -F':' '{print $1}'`
# Pad single digits with a space to align on the parenthesis
if [[ $ENTNUM -lt 10 ]]; then
ENTNUM=" $ENTNUM"
fi
ENTNAME=`echo $ENTRY | awk -F':' '{print $2}'`
# Write out the menu choice
echo -n "${ENTNUM})$ENTNAME"
# Increment the row counter
let row=$row+1
# Position the cursor for the next write
if [[ $row -lt $COLHEIGHT ]]; then
#If the row is less than the COLHEIGHT
#Set the cursor to the beginning of the line
tput cr
#And drop it down one row
tput cud1
elif [[ $row -eq $COLHEIGHT ]]; then
#If the number of lines written is equal to the COLHEIGHT we need
# to start another column.
#move back up to the first row
for (( i=1; i<$COLHEIGHT; i++ )); do
tput cuu1
done
#add COLWIDTH to the column width so the cursor will start the correct
#number of spaces to the right
let col=$col+$COLWIDTH
# Set the cursor to the beginning of the line
tput cr
#reset the row counter so we know when we reach the next row
row=0
fi
done
#Set the final cursor position beneath the menu
let finalmove=$COLHEIGHT-$row
tput cr
for (( i=1; i<=$finalmove; i++ )); do
tput cud1
done
else
echo "FAILURE"
fi
}
#-----------------
# Function: f_IsNetUp
#-----------------
# Attempts to determine if the network is functional by pinging
# the default gateway. While it is possible to have rudimentary
# network without a default gateway, this seeks to answer whether
# we have "full" networking with routing etc...
#-----------------
# Usage: f_IsNetUp
#-----------------
# Returns: YES | NO
f_IsNetUp () {
RESULT=NO
DEFAULT_GW=`f_FindDefaultGW`
TO=10
WAIT=0
FLAG=/tmp/inu${TS}
# If you don't have a default gateway, consider the network down
if [[ $DEFAULT_GW != FAILURE ]]; then
# The ping check has to be backgrounded to make sure that we can kill it if it hangs
# And because it is backgrounded it is difficult to read a variable set by it, so we're touching
# A file instead
if [[ -n `ping -w 1 -c 2 -q $DEFAULT_GW 2>&1 | egrep ' 0% packet| 50% packet'` ]]; then touch $FLAG; fi &
if [[ -n `/sbin/arping -c3 $DEFAULT_GW 2>&1 | tail -1 | egrep -v ' 0 response'` ]]; then touch $FLAG; fi &
while [[ -n `jobs -l` ]]; do
jobs 2>&1 > /dev/null
sleep 1
let WAIT=$WAIT+1
if [[ $WAIT -ge $TO ]]; then
JPID=`jobs -l | awk '{print $2}'`
if [[ -n $JPID ]]; then kill -9 $JPID; fi
fi
done
fi
# If the file got touched then the network is up, in addition to
# Changing the flag, we need to remove the file for future checks
if [[ -f $FLAG ]]; then /bin/rm $FLAG; RESULT=YES; fi
echo $RESULT
}
#-----------------
# Function: f_AtWest
#-----------------
# Attempts to determine if we are on the West Network
# This should not be run unless basic network connectivity
# has been determined
#-----------------
# Usage: f_AtWest
#-----------------
# Returns: TRUE | FALSE
f_AtWest () {
RESULT=FALSE
TO=5
FLAG=/tmp/aw${TS}
#NSLIST="172.30.9.151 172.30.94.108 10.62.21.10"
#NSLIST="10.0.0.210 10.0.35.210 10.62.21.10"
NSLIST="10.70.1.148 10.65.116.124 10.166.128.52 10.64.10.152 10.70.1.60"
for NS in $NSLIST; do
WAIT=0
if [[ ! -f $FLAG ]]; then
if [[ -n `nslookup $NS $NS | egrep 'wic.west.com|icallinc.com|west.com'` ]]; then touch $FLAG; fi &
while [[ -n `jobs -l` ]]; do
jobs 2>&1 > /dev/null
if [[ $WAIT -ge $TO ]]; then
JPID=`jobs -l | grep 'nslookup' | awk '{print $2}'`
if [[ -n $JPID ]]; then kill -9 $JPID; fi
fi
sleep 1
let WAIT=$WAIT+1
done
fi
done
# If the file got touched then we're on the west network, in addition to
# Changing the flag, we need to remove the file for future checks
if [[ -f $FLAG ]]; then /bin/rm $FLAG; RESULT=TRUE; fi
echo $RESULT
}
#-----------------
# Function: f_FindPubIF
#-----------------
# Attempts to determine which interface to use as the public interface.
# It will check the following items in order to determine which to use:
# -The interface assigned to the default gateway
# -The first interface with a real IP plumbed up
# -Network configuration files
# If there is no existing public interface, it will then try to determine
# which interface to use as the public interface. It will do so by
# checking link status.
#
#-----------------
# Usage: f_FindPubIF
#-----------------
# Returns: <IFNAME> | FAILURE
f_FindPubIF () {
CONFDIR=/etc/sysconfig/network-scripts
# Gather some information
GW=`f_FindDefaultGW`
PUBIP=`f_FindPubIP`
# Get a list of all of the eligible interfaces currently active
IFLIST=
for IF in `/sbin/ifconfig -a | egrep -v "^ |^$|lo|usb" | awk '{print $1}' | awk -F':' '{print $1}'`; do
IFLIST="$IFLIST $IF"
done
# Get a list of all of the config files that have IPADDR set
IFCFGLIST=
for f in `ls $CONFDIR | grep ^ifcfg- | egrep -v "\.|ifcfg-lo|ifcfg-usb*"`; do
if [[ -n `grep "^IPADDR=" ${CONFDIR}/${f}` ]]; then
IFCFGLIST="$IFCFGLIST $f"
fi
done
# Attempt one - which interface is assigned to the default gateway?
if [[ $GW != FAILURE ]]; then
PUBIF=`/sbin/route | grep "$GW" | awk '{print $NF}'`
fi
# Attempt two - if there IS a public IP, which interface is it attached to?
if [[ -z $PUBIF ]] && [[ $PUBIP != FAILURE ]]; then
for IF in $IFLIST; do
if [[ `f_IPforIF $IF` == $PUBIP ]]; then
PUBIF=$IF
fi
done
fi
# Attempt three - do any of the valid interfaces have an IP in the same subnet as the gateway?
if [[ -z $PUBIF ]] && [[ $GW != FAILURE ]]; then
FTO=`echo $GW | awk -F'.' '{print $1"."$2"."$3"."}'`
for IF in $IFLIST; do
IFIP=`f_IPforIF $IF`
if [[ -z $PUBIF ]] && [[ -n `echo $IFIP | grep "^${FTO}"` ]]; then
PUBIF=$IF
fi
done
fi
# Attempt four - what is the first interface with an ip plumbed up on it?
if [[ -z $PUBIF ]]; then
for IF in $IFLIST; do
IFIP=`f_IPforIF $IF`
if [[ -z PUBIF ]] && [[ -n `echo $IFIP| grep -v 'NONE|127.0.0.1'` ]]; then
PUBIF=$IF
fi
done
fi
# At this point it's safe to say that there is no recognizable network status
# Or configuration from which to pick the public interface.
# Attempt five - what is the first interface with an active link?
if [[ -z $PUBIF ]]; then
for IF in $IFLIST; do
# Sometimes the interface has to be brought up before it will properly
# report a link. So we'll check each interface to see if it's up, and if
# not, we'll plumb it before looking for a link.
WASDOWN=
if [[ -z `/sbin/ifconfig | egrep -v '^$|^ ' | grep "^$IF"` ]]; then
/sbin/ifconfig $IF up
sleep 1
WASDOWN=YES
fi
if [[ -z $CANDIDATE ]] && [[ -n `/sbin/ethtool $IF 2>&1 | grep -i 'Link detected:' | grep -i 'yes'` ]]; then
CANDIDATE=$IF
CANDIDATE_CFG="${CONFDIR}/ifcfg-${CANDIDATE}"
# Bonding drivers often lie about their link status, so we have to
# disregard the results we get back from them. We'll check the real
# interface later to see if it's part of a bond.
if [[ -n `echo $IF | grep "^bond"` ]]; then
unset CANDIDATE
elif [[ -s $CANDIDATE_CFG ]] && [[ -n `grep "^MASTER=" $CANDIDATE_CFG` ]]; then
CANDIDATE=`grep "^MASTER=" $CANDIDATE_CFG | awk -F'=' '{print $2}'`
fi
fi
if [[ $WASDOWN == YES ]]; then
/sbin/ifconfig $IF down
fi
done
if [[ -n $CANDIDATE ]]; then
PUBIF=$CANDIDATE
fi
fi
# At this point, if we still haven't found the public interface, there is nothing
# to distinguish one interface from another so we'll admit defeat.
if [[ -z $PUBIF ]]; then
RESULT=FAILURE
else
RESULT=$PUBIF
fi
echo $RESULT
}
#-----------------
# Function: f_AskPubIF
#-----------------
# Interactive
# Scans the system for usable ethernet devices
# then displays the devide names and descriptions
# and asks the user to choose a device
# NOTE: this function will not exit until a valid
# selection has been made
#-----------------
# Usage: f_AskPubIF
#-----------------
# Returns: <Ethernet Device Name>
f_AskPubIF () {
unset PUBIF
echo "Please select a public interface from the following:" 1>&2
echo "" 1>&2
# Use the name of the first interface as a default
FIRST_IF=`/sbin/ifconfig -a | egrep -v "^ |^$|lo|usb|sit" | awk '{print $1}' | head -1`
# Look at each interface that currently has a driver loaded
for IF in `/sbin/ifconfig -a | egrep -v "^ |^$|lo|usb|sit" | awk '{print $1}'`; do
# If we see a bonded device, display the names of its slaves as a description
if [[ -n `echo $IF | grep bond` ]]; then
unset SLAVES
for SLAVE in `grep "MASTER=${IF}" /etc/sysconfig/network-scripts/ifcfg-eth*`; do
SLAVES="$SLAVES `echo $SLAVE | awk -F'ifcfg-' '{print $2}' | awk -F':' '{print $1}'`"
done
IF_DESC="( slaves:$SLAVES )"
# If we see something that is NOT a bond or virutal device...
else
# Get the PCI address from ethtool
IF_PCI=`/sbin/ethtool -i $IF | grep bus-info | awk -F':' '{print $3":"$4}'`
# ...and the description from lspci
IF_DESC="(`/sbin/lspci | grep "^$IF_PCI" | awk -F': ' '{print $NF}' | cut -c 1-60 | sed -e 's/($//;s/ $//'`...)"
fi
# Display the device and description
echo " $IF $IF_DESC" 1>&2
done
echo " " 1>&2
while [[ -z $PUBIF ]]; do
read -p "Public Interface [$FIRST_IF]: " GPUBIF
if [[ -z $GPUBIF ]]; then
PUBIF=$FIRST_IF
elif [[ -z `/sbin/ifconfig -a | egrep -v "^ |^$|lo|usb|sit" | grep ^${GPUBIF}` ]]; then
echo "Error: \"$GPUBIF\" is not a valid selection."
read -p " Press enter to try again." JUNK
UNSET GPUBIF
tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el; tput cuu1; tput el
else
PUBIF=$GPUBIF
fi
done
echo $PUBIF
}
#-----------------
# Function: f_SpinningCountdown
#-----------------
# Displays a visual countdown bar that shrinks as it approaches
# zero and displays a spinner to increase visibility. The idea
# is to give as much opportunity to cancel as possible before
# doing some disruptive action.
#-----------------
# Usage: f_SpinningCountdown <SEC>
#-----------------
# Returns: NULL
f_SpinningCountdown() {
COUNT=$1
MCOUNT=$COUNT
while [[ $MCOUNT -ge 0 ]]; do
DASHES=
for (( i=1; i<=$MCOUNT; i++ )); do
DASHES="${DASHES}="
done
echo -n "[$DASHES"
echo -n "$MCOUNT]"
tput sc
# Each character is displayed for 1/16th of a second
# Do all of them 4 times for one whole second
for j in {1..4}; do
tput rc
echo -n "|"
sleep .053
tput rc
echo -n "/"
sleep .053
tput rc
echo -n "-"
sleep .053
tput rc
echo -n '\'
sleep .053
done
let MCOUNT=$MCOUNT-1
tput cr
tput el
done
}
#-----------------
# Function: f_PathOfScript
#-----------------
# Returns the location of the script, irrespective of where it
# was launched. This is useful for scripts that look for files
# in their current directory, or in relative paths from it
#
#-----------------
# Usage: f_PathOfScript
#-----------------
# Returns: <PATH>
f_PathOfScript () {
unset RESULT
# if $0 begins with a / then it is an absolute path
# which we can get by removing the scipt name from the end of $0
if [[ -n `echo $0 | grep "^/"` ]]; then
BASENAME=`basename $0`
RESULT=`echo $0 | sed 's/'"$BASENAME"'$//g'`
# if this isn't an absolute path, see if removing the ./ from the
# beginning of $0 results in just the basename - if so
# the script is being executed from the present working directory
elif [[ `echo $0 | sed 's/^.\///g'` == `basename $0` ]]; then
RESULT=`pwd`
# If we're not dealing with an absolute path, we're dealing with
# a relative path, which we can get with pwd + $0 - basename
else
BASENAME=`basename $0`
RESULT="`pwd`/`echo $0 | sed 's/'"$BASENAME"'$//g'`"
fi
echo $RESULT
}
#-----------------
# Function: f_GetVendor
#-----------------
# Returns the name of the hardware vendor for the current server
# Current possible responses:
# IBM
# HP
# VMWARE
# DELL
# UNK
#-----------------
# Usage: f_GetVendor
#-----------------
# Returns: <Vendor>
f_GetVendor () {
DMIDECODE=/usr/sbin/dmidecode
unset RESULT
if [[ -n `$DMIDECODE -t system 2>&1 | grep Manufacturer: | grep -i VMWARE` ]]; then
RESULT=VMWARE
elif [[ -n `$DMIDECODE -t system 2>&1 | grep Manufacturer: | grep -i IBM` ]]; then
RESULT=IBM
elif [[ -n `$DMIDECODE -t system 2>&1 | grep Manufacturer: | grep -i HP` ]]; then
RESULT=HP
elif [[ -n `$DMIDECODE -t system 2>&1 | grep Manufacturer: | grep -i DELL` ]]; then
RESULT=DELL
elif [[ -n `$DMIDECODE -t system 2>&1 | grep Manufacturer: | grep -i Cisco` ]]; then
RESULT=CISCO
else
RESULT=UNK
fi
echo $RESULT
}
#-----------------
# Function: f_GetImageServerIP
#-----------------
# Attempts to ascertain the correct IP address for the image server
#-----------------
# Usage: f_GetImageServerIP
#-----------------
# Returns: <IP>
f_GetImageServerIP () {
IMGSRVPHN=imageprimary.svc.west.com
IMGSRVMN=linux157.wic.west.com
HCIP=172.30.113.167
unset IMGSRVIP
# First try the private hostname
IMGSRVIP=`getent hosts $IMGSRVPHN | awk '{print $1}'`
# Next try the machine hostname
if [[ -z $IMGSRVIP ]]; then
IMGSRVIP=`getent hosts $IMGSRVMN | awk '{print $1}'`
fi
# Last fall back on the hard-coded IP
if [[ -z $IMGSRVIP ]]; then
IMGSRVIP=$HCIP
fi
echo $IMGSRVIP
}
#-----------------
# Function: f_GetPhysicalDriveCount
#-----------------
# Attempts to ascertain the number of physical drives installed
# on a server. Negative numbers represent error codes
# Error Codes:
# -1 unknown vendor
# -2 missing utility
# -3 unknown adapter
#-----------------
# Usage: f_GetPhysicalDriveCount
#-----------------
# Returns: <Drive Count> | <Error Code>
f_GetPhysicalDriveCount () {
VENDOR=`f_GetVendor`
# Figure out architecure
if [[ -n `uname -m | grep x86_64` ]]; then
ARCH=64
else
ARCH=32
fi
# Break down the process by vendor
if [[ $VENDOR == HP ]]; then
if [[ ! -x /usr/sbin/hpacucli ]]; then
PDC="-2"
else
PDC=`/usr/sbin/hpacucli controller all show config | grep physicaldrive | wc -l`
fi
elif [[ $VENDOR == IBM ]]; then
# Check for adaptec RAID
if [[ -n `/sbin/lspci | grep -i raid | grep -i adaptec` ]]; then
ARCCONF=/sbin/arcconf
if [[ ! -x $ARCCONF ]]; then
PDC="-2"
else
CC=`$ARCCONF getconfig 9 | grep "Controllers found" | awk '{print $NF}'`
tdc=0
for (( c=1; c <= CC; c++ )); do
dc=`$ARCCONF getconfig $c | grep -i "Device is a Hard drive" | wc -l`
let tdc=$tdc+$dc
done
PDC=$tdc
fi
# Check for LSI RAID
elif [[ -n `/sbin/lspci | grep -i raid | grep -i LSI` ]]; then
if [[ $ARCH == 64 ]]; then
MEGACLI=/opt/MegaRAID/MegaCli/MegaCli64
else
MEGACLI=/opt/MegaRAID/MegaCli/MegaCli
fi
tdc=0
for dc in `$MEGACLI -PDGetNum -aALL | grep "Physical Drives" | awk '{print $NF}'`; do
let tdc=$tdc+$dc
done
PDC=$tdc
# Check for older LSI storage controller
elif [[ -n `/sbin/lspci | grep -i storage | grep -i LSI` ]]; then
PDC=`cat /proc/scsi/scsi | sed ':a;N;$!ba;s/\n//g; s/Host:/\nHost:/g' | grep Direct-Access | egrep -v 'LSI|ROM' | wc -l`
else
PDC="-3"
fi
elif [[ $VENDOR == DELL ]]; then
# Best guess for DELL - has not been tested
PDC=`cat /proc/scsi/scsi | sed ':a;N;$!ba;s/\n//g; s/Host:/\nHost:/g' | grep Direct-Access | egrep -v 'Dell|ROM' | wc -l`
elif [[ $VENDOR == VMWARE ]]; then
# For practical purposes, the physical drive count is the same as the logical drive count.
PDC=`fdisk -l 2>&1 | grep ^Disk | egrep -v 'doesn|mapper|identifier' | wc -l`
else
PDC="-1"
fi
echo $PDC
}
#------------------------------------------------------
# Function f_IsHostValid
# Purpose: Check if a hostname can be resolved to an IP
# if that IP is live and listening on specified port
# Usage: f_IsHostValid <hostname> <port>
# Returns: 0 if the host is valid
# 1 if the host is a hostname and failed a DNS lookup
# 2 if the host failed a ping test
# 3 if the host is not listening to <port>
# 4 if the port is not valid or missing
#------------------------------------------------------
f_IsHostValid () {
hostToCheck=$1
portOfInterest=$2
HostValid=TRUE
ExitCode=0
# Check to see if we were passed an IP or a hostname
#if [[ ! `echo $hostToCheck | sed 's/\.//g'` =~ ^[0-9]+$ ]]; then
if [[ -z `echo $hostToCheck | sed 's/\.//g' | egrep "^[0-9]+$"` ]]; then
# Host does not appear to be an IP so verify valid lookup
if [[ -z `getent hosts $hostToCheck` ]]; then
ExitCode=1
HostValid=FALSE
fi
fi