forked from alhazred/solaris-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_fc_config.sh
6591 lines (5507 loc) · 192 KB
/
check_fc_config.sh
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
#!/usr/bin/ksh
#
# **** Note: The main code starts after the line containing "# main:" ****
# The main code for your script should start after "# main - your code"
# Function statt after the line containing "# functions: "
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006-2014 Bernd Schemmer All rights reserved.
# Use is subject to license terms.
#
# Notes:
#
# - use "check_fc_config.sh {-v} {-v} {-v} -h" to get the usage help
#
# - replace "scriptt.sh" with the name of your script
# - change the parts marked with "???" and "??" to your need
#
# - use "check_fc_config.sh -H 2>check_fc_config.sh.doc" to get the documentation
#
# - use "check_fc_config.sh -X 2>check_fc_config.sh.examples.doc" to get some usage examples
#
# - this is a Kornshell script - it may not function correctly in other shells
# - the script was written and tested with ksh88 but should also work in ksh93
# The script should also work in bash -- but that is NOT completly tested
#
# ??? Add usage examples here; the lines should start with ##EXAMPLE##
#
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # create the documentation for the script
##EXAMPLE##
##EXAMPLE## ./${__SCRIPTNAME} -H 2>./${__SCRIPTNAME}.doc
##EXAMPLE##
##EXAMPLE## # get the verbose usage for the script
##EXAMPLE##
##EXAMPLE## ./${__SCRIPTNAME} -v -v -h
##EXAMPLE##
##EXAMPLE## # write a config file for the script
##EXAMPLE##
##EXAMPLE## ./${__SCRIPTNAME} -C
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # use another config file
##EXAMPLE##
##EXAMPLE## CONFIG_FILE=myconfig.conf ./${__SCRIPTNAME}
##EXAMPLE##
##EXAMPLE## # use no config file at all
##EXAMPLE##
##EXAMPLE## CONFIG_FILE=none ./${__SCRIPTNAME}
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # write all output (STDOUT and STDERR) to the file
##EXAMPLE## # /var/tmp/mylog.txt
##EXAMPLE##
##EXAMPLE## __TEE_OUTPUT_FILE=/var/tmp/mylog.txt ./${__SCRIPTNAME} -T
##EXAMPLE##
##EXAMPLE##
##EXAMPLE## # create a dump of the environment variables in the files
##EXAMPLE## # /tmp/${__SCRIPTNAME}.envvars.\$\$
##EXAMPLE## # /tmp/${__SCRIPTNAME}.exported_envvars.\$\$
##EXAMPLE## # (\$\$ is the PID of the process running the script)
##EXAMPLE##
##EXAMPLE## __CREATE_DUMP=1 ./${__SCRIPTNAME}
##EXAMPLE##
##EXAMPLE## # create a dump of the environment variables in the files
##EXAMPLE## # /var/tmp/debug/${__SCRIPTNAME}.envvars.\$\$
##EXAMPLE## # /var/tmp/debug/${__SCRIPTNAME}.exported_envvars.\##EXAMPLE## # (\$\$ is the PID of the process running the script)
##EXAMPLE## # (\$\$ is the PID of the process running the script)
##EXAMPLE##
##EXAMPLE## __CREATE_DUMP=/var/tmp/debug ./${__SCRIPTNAME}
##EXAMPLE##
#
# Note: The escape character in the command below is only for the usage of check_fc_config.sh with the "-X" parameter!
#
##EXAMPLE## # use logger instead of echo to print the messages
##EXAMPLE##
##EXAMPLE## LOGMSG_FUNCTION=\"logger -s -p user.info check_fc_config.sh\" ./check_fc_config.sh
##EXAMPLE##
# -----------------------------------------------------------------------------
####
#### check_fc_config.sh - check the fiber channel configuration in Solaris
####
#### Author: Bernd Schemmer ([email protected])
####
#### Version: see variable ${__SCRIPT_VERSION} below
#### (see variable ${__SCRIPT_TEMPLATE_VERSION} for the template version used)
####
#### Supported OS: Solaris SPARC and Solaris x86
####
####
#### Description
#### -----------
####
#### This script retrieves the output of various commands to view the current
#### fiber channel configuration and generates a table with the most important
#### information regarding the fiber channel configuration
##C#
##C# Configuration file
##C# ------------------
##C#
##C# This script supports a configuration file called <scriptname>.conf.
##C# The configuration file is searched in the working directory,
##C# the home directory of the user executing this script and in /etc
##C# (in this order).
##C#
##C# The configuration file is read before the parameter are processed.
##C#
##C# To override the default config file search set the variable
##C# CONFIG_FILE to the name of the config file to use.
##C#
##C# e.g. CONFIG_FILE=/var/myconfigfile ./check_fc_config.sh
##C#
##C# To disable the use of a config file use
##C#
##C# CONFIG_FILE=none ./check_fc_config.sh
##C#
##C# See the variable __CONFIG_PARAMETER below for the possible entries
##C# in the config file.
##C#
####
#### Predefined parameter
#### --------------------
####
#### see the subroutines ShowShortUsage and ShowUsage
####
#### Note: The current version of the script template can be found here:
####
#### http://bnsmb.de/solaris/scriptt.html
####
####
##T# Troubleshooting support
##T# -----------------------
##T#
##T# Use
##T#
##T# __CREATE_DUMP=<anyvalue|directory> <yourscript>
##T#
##T# to create a dump of the environment variables on program exit.
##T#
##T# e.g
##T#
##T# __CREATE_DUMP=1 ./check_fc_config.sh
##T#
##T# will create a dump of the environment variables in the files
##T#
##T# /tmp/check_fc_config.sh.envvars.$$
##T# /tmp/check_fc_config.sh.exported_envvars.$$
##T#
##T# before the script ends
##T#
##T# __CREATE_DUMP=/var/tmp/debug ./check_fc_config.sh
##T#
##T# will create a dump of the environment variables in the files
##T#
##T# /var/tmp/debug/check_fc_config.sh.envvars.$$
##T# /var/tmp/debug/check_fc_config.sh.exported_envvars.$$
##T#
##T# before the script ends (the target directory must already exist).
##T#
##T# Note that the dump files will always be created in case of a syntax
##T# error. To set the directory for these files use either
##T#
##T# export __DUMPDIR=/var/tmp/debug
##T# ./check_fc_config.sh
##T#
##T# or define __DUMPDIR in the script.
##T#
##T# To suppress creating the dump file in case of a syntax error add
##T# the statement
##T#
##T# __DUMP_ALREADY_CREATED=0
##T#
##T# to your script
##T#
##T# Use
##T#
##T# CreateDump <uniqdirectory> [filename_add]
##T#
##T# to manually create the dump files from within the script.
##T#
##T# e.g.
##T#
##T# CreateDump /var/debug
##T#
##T# will create the files
##T#
##T# /var/debug/check_fc_config.sh.envvars.$$
##T# /var/debug/check_fc_config.sh.exported_envvars.$$
##T#
##T# CreateDump /var/debug pass2.
##T#
##T# will create the files
##T#
##T# /var/debug/check_fc_config.sh.envvars.pass2.$$
##T# /var/debug/check_fc_config.sh.exported_envvars.pass2.$$
##T#
#### Note:
#### The default action for the signal handler USR1 is
#### "Create an environment dump in /var/tmp"
#### The filenames for the dumps are
####
#### /var/tmp/<scriptname>.envvars.dump_no_<no>_<PID>
#### /var/tmp/<scriptname>.exported_envvars.dump_no_<no>_<PID>
####
#### where <no> is a sequential number, <PID> is the PID of the
#### process with the script, and <scriptname> is the name of the
#### script without the path.
####
####
#### User defined signal handler
#### ---------------------------
####
#### You can define various SIGNAL handlers to process signals received
#### by the script.
####
#### All SIGNAL handlers can use these variables:
####
#### __TRAP_SIGNAL -- the catched trap signal
#### INTERRUPTED_FUNCTION -- the function interrupted by the signal
####
####
#### To define one signal handler for all signals do:
####
#### __GENERAL_SIGNAL_FUNCTION=<signalhandler_name>
####
#### To define unique signal handler for the various signals do:
####
#### __SIGNAL_SIGUSR1_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGUSR2_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGHUP_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGINT_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGQUIT_FUNCTION=<signalhandler_name>
#### __SIGNAL_SIGTERM_FUNCTION=<signalhandler_name>
####
#### If both type of signal handler are defined the script first
#### executes the general signal handler.
#### If this handler returns 0 the handler for the catched signal will
#### also be executed (else not).
#### If the unique handler for the signal ends with 0 the default signal
#### handler for this signal will also be executed (else not)
####
####
#### Credits
#### -------
####
#### Hints regarding the code to create the lockfile
#### wpollock (http://wikis.sun.com/display/~wpollock)
####
#### Source for the function PrintWithTimeStamp (in version 2.x and newer):
#### http://unix.stackexchange.com/questions/26728/prepending-a-timestamp-to-each-line-of-output-from-a-command
####
#### Andreas Obermaier for a security issue in the lockfile routine
#### (see history section 1.22.45 07.06.2012)
####
#### The code used in executeCommandAndLog is from
#### http://www.unix.com/unix-dummies-questions-answers/13018-exit-status-command-pipe-line.html#post47559
####
####
#### History:
#### --------
#### 29.04.2013 v1.0.0 /bs
#### initial release
#### 06.08.2013 v1.1.0 /bs
#### script rewritten using scriptt.sh
#### added new information to the output
#### 15.08.2013 v1.2.0 /bs
#### corrected the disk/lun/tape infos
#### added the parameter -L outputFile
#### 18.08.2013 v1.2.1 /bs
#### added support for Guest LDoms
#### 26.11.2013 v1.2.2 /bs
#### added code to get the PCI slots used on x86 machines
#### (Note: tested on an IBM xSeries and an Oracle Server only!)
#### 03.09.2014 v2.0.0 /bs
#### script rewritten using the template scriptt.sh v2.0.0.7
#### changed the code for x86 machines some how
#### added some debug switches (use -D help to list the known debug switches)
#### added the parameter -s (show slot usage)
#### 17.09.2014 v2.0.1 /bs
#### PCI Slot information in the output file for -L for x86 machines was still wrong -- corrected
#### 03.10.2014 v2.1.0 /bs
#### the script now also prints the PCI Slot usage for SPARC Tx machines
#### added the parameter -P (show PCI slot usage only)
#### 19.02.2015 v2.2.0 /bs
#### corrected a bug in the x86 slot retrieval routine
#### tested the script on a Oracle X4-2L, added partial support for the Oracle x4800
#### 11.01.2019 v2.3.0 /bs
#### added support for Solaris 11 ; tested on SPARC S7-2L
#### code for the trap handler rewritten (it did not work in the ksh from Solaris 11)
####
####
##V# script template History
##V# -----------------------
##V# 1.22.0 08.06.2006 /bs (BigAdmin Version 1)
##V# public release; starting history for the script template
##V#
##V# 1.22.1 12.06.2006 /bs
##V# added true/false to CheckYNParameter and ConvertToYesNo
##V#
##V# 1.22.2. 21.06.2006 /bs
##V# added the parameter -V
##V# added the use of environment variables
##V# added the variable __NO_TIME_STAMPS
##V# added the variable __NO_HEADERS
##V# corrected a bug in the function executeCommandAndLogSTDERR
##V# added missing return commands
##V#
##V# 1.22.3 24.06.2006 /bs
##V# added the function StartStop_LogAll_to_logfile
##V# added the variable __USE_TTY (used in AskUser)
##V# corrected an spelling error (dev/nul instead of /dev/null)
##V#
##V# 1.22.4 06.07.2006 /bs
##V# corrected a bug in the parameter error handling routine
##V#
##V# 1.22.5 27.07.2006 /bs
##V# corrected some minor bugs
##V#
##V# 1.22.6 09.08.2006 /bs
##V# corrected some minor bugs
##V#
##V# 1.22.7 17.08.2006 /bs
##V# add the CheckParameterCount function
##V# added the parameter -T
##V# added long parameter support (e.g --help)
##V#
##V# 1.22.8 07.09.2006 /bs
##V# added code to save the env variable LANG and set it temporary to C
##V#
##V# 1.22.9 20.09.2006 /bs
##V# corrected code to save the env variable LANG and set it temporary to C
##V#
##V# 1.22.10 21.09.2006 /bs
##V# cleanup comments
##V# the number of temporary files created automatically is now variable
##V# (see the variable __NO_OF_TEMPFILES)
##V# added code to install the trap handler in all functions
##V#
##V# 1.22.11 19.10.2006 /bs
##V# corrected a minor bug in AskUser (/c was not interpreted by echo)
##V# corrected a bug in the handling of the parameter -S (-S was ignored)
##V#
##V# 1.22.12 31.10.2006 /bs
##V# added the variable __REQUIRED_ZONE
##V#
##V# 1.22.13 13.11.2006 /bs
##V# the template now uses TMP or TEMP if set for the temporary files
##V#
##V# 1.22.14 14.11.2006 /bs
##V# corrected a bug in the function AskUser (the default was y not n)
##V#
##V# 1.22.15 21.11.2006 /bs
##V# added initial support for other Operating Systems
##V#
##V# 1.22.16 05.07.2007 /bs
##V# enhanced initial support for other Operating Systems
##V# Support for other OS is still not fully tested!
##V#
##V# 1.22.17 06.07.2007 /bs
##V# added the global variable __TRAP_SIGNAL
##V#
##V# 1.22.18 01.08.2007 /bs
##V# __OS_VERSION and __OS_RELEASE were not set - corrected
##V#
##V# 1.22.19 04.08.2007 /bs
##V# wrong function used to print "__TRAP_SIGNAL is \"${__TRAP_SIGNAL}\"" - fixed
##V#
##V# 1.22.20 12.09.2007 /bs
##V# the script now checks the ksh version if running on Solaris
##V# made some changes for compatibility with ksh93
##V#
##V# 1.22.21 18.09.2007 /bs (BigAdmin Version 2)
##V# added the variable __FINISHROUTINES
##V# changed __REQUIRED_ZONE to __REQUIRED_ZONES
##V# added the variable __KSH_VERSION
##V# reworked the trap handling
##V#
##V# 1.22.22 23.09.2007 /bs
##V# added the signal handling for SIGUSR1 and SIGUSR2 (variables __SIGUSR1_FUNC and __SIGUSR2_FUNC)
##V# added user defined function for the signals HUP, BREAK, TERM, QUIT, EXIT, USR1 and USR2
##V# added the variables __WARNING_PREFIX, __ERROR_PREFIX, __INFO_PREFIX, and __RUNTIME_INFO_PREFIX
##V# the parameter -T or --tee can now be on any position in the parameters
##V# the default output file if called with -T or --tee is now
##V# /var/tmp/${0##*/}.$$.tee.log
##V#
##V# 1.22.23 25.09.2007 /bs
##V# added the environment variables __INFO_PREFIX, __WARNING_PREFIX,
##V# __ERROR_PREFIX, and __RUNTIME_INFO_PREFIX
##V# added the environment variable __DEBUG_HISTFILE
##V# reworked the function to print the usage help :
##V# use "-h -v" to view the extented usage help and use "-h -v -v" to
##V# view the environment variables used also
##V#
##V# 1.22.24 05.10.2007 /bs
##V# another minor fix for ksh93 compatibility
##V#
##V# 1.22.25 08.10.2007 /bs
##V# only spelling errors corrected
##V#
##V# 1.22.26 19.11.2007 /bs
##V# only spelling errors corrected
##V#
##V# 1.22.27 29.12.2007 /bs
##V# improved the code to create the lockfile (thanks to wpollock for the info; see credits above)
##V# improved the code to create the temporary files (thanks to wpollock for the info; see credits above)
##V# added the function rand (thanks to wpollock for the info; see credits above)
##V# the script now uses the directory name saved in the variable $TMPDIR for temporary files
##V# if it's defined
##V# now the umask used for creating temporary files can be changed (via variable __TEMPFILE_UMASK)
##V#
##V# 1.22.28 12.01.2008 /bs
##V# corrected a syntax error in the show usage routine
##V# added the function PrintWithTimestamp (see credits above)
##V#
##V# 1.22.29 31.01.2008 /bs
##V# there was a bug in the new code to remove the lockfile which prevented
##V# the script from removing the lockfile at program end
##V# if the lockfile already exist the script printed not the correct error
##V# message
##V#
##V# 1.22.30 28.02.2008 /bs
##V# Info update: executeCommandAndLog does NOT return the RC of the executed
##V# command if a logfile is defined
##V# added inital support for CYGWIN
##V# (tested with CYGWIN_NT-5.1 v..1.5.20(0.156/4/2)
##V# Most of the internal functions are NOT tested yet in CYGWIN
##V# GetCurrentUID now supports UIDs greater than 254; the function now prints the UID to STDOUT
##V# Corrected bug in GetUserName (only a workaround, not the solution)
##V# now using printf in the AskUserRoutine
##V#
##V# 1.22.30 28.02.2008 /bs
##V# The lockfile is now also deleted if the script crashes because of a syntax error or something like this
##V#
##V# 1.22.31 18.03.2008 /bs
##V# added the version number to the start and end messages
##V# an existing config file is now removed (and not read) if the script is called with -C to create a config file
##V#
##V# 1.22.32 04.04.2008 /bs
##V# minor changes for zone support
##V#
##V# 1.22.33 12.02.2009 /bs
##V# disabled the usage of prtdiag due to the fact that prtdiag on newer Sun machines needs a long time to run
##V# (-> __MACHINE_SUBTYPE is now always empty for Solaris machines)
##V# added the variable __CONFIG_FILE_FOUND; this variable contains the name of the config file
##V# read if a config file was found
##V# added the variable __CONFIG_FILE_VERSION
##V#
##V# 1.22.34 28.02.2009 /bs
##V# added code to check for the max. line no for the debug handler
##V# (an array in ksh88 can only handle up to 4096 entries)
##V# added the variable __PIDFILE
##V#
##V# 1.22.35 06.04.2009 /bs
##V# added the variables
##V# __NO_CLEANUP
##V# __NO_EXIT_ROUTINES
##V# __NO_TEMPFILES_DELETE
##V# __NO_TEMPMOUNTS_UMOUNT
##V# __NO_TEMPDIR_DELETE
##V# __NO_FINISH_ROUTINES
##V# __CLEANUP_ON_ERROR
##V# CONFIG_FILE
##V#
##V# 1.22.36 11.04.2009 /bs
##V# corrected a cosmetic error in the messages (wrong: ${TEMPFILE#} correct: ${__TEMPFILE#})
##V#
##V# 1.22.37 08.07.2011 /bs
##V# corrected a minor error with the QUIET parameter
##V# added code to dump the environment (env var __CREATE_DUMP, function CreateDump )
##V# implemented work around for missing function whence in bash
##V# added the function LogIfNotVerbose
##V#
##V# 1.22.38 22.07.2011 /bs
##V# added code to make the trap handling also work in bash
##V# added a sample user defined trap handler (function USER_SIGNAL_HANDLER)
##V# added the function SetHousekeeping to enabe or disable house keeping
##V# scriptt.sh did not write all messages to the logfile if a relative filename was used - fixed
##V# added more help text for "-v -v -v -h"
##V# now user defined signal handler can have arguments
##V# the RBAC feature (__USE_RBAC) did not work as expected - fixed
##V# added new scriptt testsuite for testing the script template on other OS and/or shells
##V# added the function SaveEnvironmentVariables
##V#
##V# 1.22.39 24.07.2011 /bs
##V# __INIT_FUNCTION now enabled for cygwin also
##V# __SHELL did not work in all Unixes - fixed
##V# __OS_FULLNAME is now also set in Solaris and Linux
##V#
##V# 1.22.40 25.07.2011 /bs
##V# added some code for ksh93 (functions: substr)
##V# Note: set __USE_ONLY_KSH88_FEATURES to ${__TRUE} to suppress using the ksh93 features
##V# The default action for the signal handler USR1 is now "Create an env dump in /var/tmp"
##V# The filenames for the dumps are
##V#
##V# /var/tmp/<scriptname>.envvars.dump_no_<no>_<PID>
##V# /var/tmp/<scriptname>.exported_envvars.dump_no_<no>_<PID>
##V#
##V# where <no> is a sequential number, <PID> is the PID of the process with the script,
##V# and <scriptname> is the name of the script without the path.
##V#
##V# 1.22.41 26.09.2011 /bs
##V# added the parameter -X
##V# disabled some ksh93 code because "ksh -x -n" using ksh88 does not like it
##V#
##V# 1.22.42 05.10.2011 /bs
##V# added the function PrintDotToSTDOUT
##V#
##V# 1.22.43 15.10.2011 /bs
##V# added support for disabling the config file feature with CONFIG_FILE=none ./scriptt.sh
##V# corrected a minor bug in SaveEnvironmentVariables
##V# corrected a bug in the function SaveEnvironmentVariables
##V# corrected a bug in getting the value for the variable ${__ABSOLUTE_SCRIPTDIR}
##V#
##V# 1.22.44 22.04.2012 /bs
##V# The script now uses nawk only if available (if not awk is used)
##V# variables are now supported in the usage examples (prefixed with ##EXAMPLE##)
##V# add a line with the current date and time to variable dumps, e.g.
##V#
##V# ### /var/tmp/scriptt.sh.exported_envvars.dump_no_0_20074 - exported environment variable dump created on Sun Apr 22 11:35:38 CEST 2012
##V#
##V# ### /var/tmp/scriptt.sh.envvars.dump_no_0_20074 - environment variable dump created on Sun Apr 22 11:35:38 CEST 2012
##V#
##V# added experimental interactive mode to the signal handler for USR2
##V# replaced /usr/bin/echo with printf
##V# added the variable LOGMSG_FUNCTION
##V#
##V# 1.22.45 07.06.2012 /bs
##V# added code to check if the symbolic link for the lockfile already exists before creating
##V# the lock file
##V#
##V# 1.22.46 27.04.2013 /bs
##V# executeCommandAndLog rewritten using coprocesses (see also credits)
##V# Info update: executeCommandAndLog does now return the RC of the executed
##V# command even if a logfile is defined
##V#
##V# -------------------------------------------------------------------
##V#
##V# 2.0.0.0 17.05.2013 /bs
##V# added the variable __GENERAL_SIGNAL_FUNCTION: This variable
##V# contains the name of a function that is called for all SIGNALs
##V# before the special SIGNAL handler is called
##V# removed the Debug Handler for single step execution (due to the
##V# length of the template it is not useful anymore; use the
##V# version 1.x of scriptt.sh if you still need the Debug Handler)
##V# function executeCommandAndLogSTDERR rewritten
##V# removed the function CheckParameterCount
##V# use lsb_release in Linux to retrieve OS infos if available
##V# minor fixes for code and comments
##V# replaced PrintWithTimeStamp with code that does not use awk
##V# isNumber replaced with code that does not use sed
##V#
##V# 2.0.0.1 06.08.2013 /bs
##V# added the variable __MACHINE_SUB_CLASS. Possible values
##V# for sun4v machines: either "GuestLDom" or "PrimaryLDom"
##V#
##V# 2.0.0.2 01.09.2013 /bs
##V# added the variables __SYSCMDS and __SYSCMDS_FILE
##V#
##V# 2.0.0.3 16.12.2013 /bs
##V# now the Log-* functions return ${__TRUE} if a message is printed
##V# and ${__FALSE} if not
##V#
##V# 2.0.0.4 01.01.2014 /bs
##V# the alias __settrap is renamed to settraps (with leading s)
##V# two new aliase are defined: __ignoretraps and __unsettraps
##V# whence function for non-ksh compatible shells rewritten
##V# without using ksh
##V# the switch -D is now used to toggle debug switches
##V# known debug switches:
##V# help -- print the usage help for -D
##V# msg -- log debug messages to /tmp/<scriptname>.<pid>.debug
##V# trace -- activate tracing to the file /tmp/<scriptname>.<pid>.trace
##V# AskUser now accepts also "yes" and "no"
##V# function IsFunctionDefined rewritten
##V# now __LOGON_USERID and __USERID are equal to $LOGNAME until I
##V# find a working solution (the code in the previous version
##V# did not work if STDIN is not a tty)
##V#
##V# 2.0.0.5 08.01.2014 /bs
##V# added the function executeFunctionIfDefined
##V#
##V# 2.0.0.6 27.01.2014 /bs
##V# added the function PrintLine
##V# added the functions GetSeconds, GetMinutes, ConvertMinutesToHours,
##V# and GetTimeStamp
##V# added the debug options fn_to_stderr, fn_to_tty, and fn_to_handle9
##V# max. return value for a function is 255 and therefor the functions
##V# for the stack and the functions pos and lastpos now abort the
##V# script if a value greater than 255 should be returned
##V# added the variables __SHEBANG, __SCRIPT_SHELL, and __SCRIPT_SHELL_OPTIONS
##V# added the function DebugShell
##V# AskUser now has a hidden shell; use "shell<return>" to call the DebugShell
##V# set __DEBUG_SHELL_IN_ASKUSER to ${__FALSE} to disable the DebugShell
##V# in AskUser
##V# added the function ConvertDateToEpoc
##V#
##V# 2.0.0.7 27.04.2014 /bs
##V# AskUser now save the last input in the variable LAST_USER_INPUT, to enter
##V# this value again use "#last"
##V# Version parameter (-V) usage enhanced: use "-v -v -V" to print also the version
##V# history; use "-v -v -v -V" to also print the template version history.
##V#
#### ----------------
#### Version variables
####
#### __SCRIPT_VERSION - the version of your script
####
####
typeset -r __SCRIPT_VERSION="v2.2.0"
####
#### __SCRIPT_TEMPLATE_VERSION - version of the script template
####
typeset -r __SCRIPT_TEMPLATE_VERSION="2.0.0.7 27.04.2014"
####
#### ----------------
####
##R# Predefined return codes:
##R# ------------------------
##R#
##R# 1 - show usage and exit
##R# 2 - invalid parameter found
##R#
##R# 210 - 232 reserved for the runtime system
##R# 233 - Can not write to handle 9
##R# 234 - The return value is greater than 255 in function x
##R# 235 - Invalid debug switch found
##R# 236 - syntax error
##R# 237 - Can not write to the debug log file
##R# 238 - unsupported Operating system
##R# 239 - script runs in a not supported zone
##R# 240 - internal error
##R# 241 - a command ended with an error (set -e is necessary to activate this trap)
##R# 242 - the current user is not allowed to execute this script
##R# 243 - invalid machine architecture
##R# 244 - invalid processor type
##R# 245 - invalid machine platform
##R# 246 - error writing the config file
##R# 247 - include script not found
##R# 248 - unsupported OS version
##R# 249 - Script not executed by root
##R# 250 - Script is already running
##R#
##R# 251 - QUIT signal received
##R# 252 - User break
##R# 253 - TERM signal received
##R# 254 - unknown external signal received
##R#
#### ----------------
#### Used environment variables
####
#
# The variable __USED_ENVIRONMENT_VARIABLES is used in the function ShowUsage
#
__USED_ENVIRONMENT_VARIABLES="
#### __DEBUG_CODE
#### __RT_VERBOSE_LEVEL
#### __QUIET_MODE
#### __VERBOSE_MODE
#### __VERBOSE_LEVEL
#### __OVERWRITE_MODE
#### __USER_BREAK_ALLOWED
#### __NO_TIME_STAMPS
#### __NO_HEADERS
#### __USE_COLORS
#### __USE_RBAC
#### __RBAC_BINARY
#### __TEE_OUTPUT_FILE
#### __INFO_PREFIX
#### __WARNING_PREFIX
#### __ERROR_PREFIX
#### __RUNTIME_INFO_PREFIX
#### __NO_CLEANUP
#### __NO_EXIT_ROUTINES
#### __NO_TEMPFILES_DELETE
#### __NO_TEMPMOUNTS_UMOUNT
#### __NO_TEMPDIR_DELETE
#### __NO_FINISH_ROUTINES
#### __CLEANUP_ON_ERROR
#### __CREATE_DUMP
#### __DUMP_ALREADY_CREATED
#### __DUMPDIR
#### __USE_ONLY_KSH88_FEATURES
#### CONFIG_FILE
"
####
#
# binaries and scripts used in this script:
#
# awk basename cat cp cpio cut date dd dirname egrep expr find grep id
# ln ls nawk pwd perl
# reboot rm sed sh tee touch tty umount uname who zonename
#
# /usr/bin/pfexec
# /usr/ucb/whoami or $( whence whoami )
# /usr/openwin/bin/resize or $( whence resize )
#
# AIX: oslevel
#
# Linux: lsb_release
#
# -----------------------------------------------------------------------------
# variables for the trap handler
__FUNCTION="main"
#
# Note: The usage of the variable LINENO is different in the various ksh versions
#
# alias to install the trap handler
#
alias __settraps="
trap 'GENERAL_SIGNAL_HANDLER SIGHUP \${LINENO} \${__FUNCTION}' 1
trap 'GENERAL_SIGNAL_HANDLER SIGINT \${LINENO} \${__FUNCTION}' 2
trap 'GENERAL_SIGNAL_HANDLER SIGQUIT \${LINENO} \${__FUNCTION}' 3
trap 'GENERAL_SIGNAL_HANDLER SIGTERM \${LINENO} \${__FUNCTION}' 15
trap 'GENERAL_SIGNAL_HANDLER SIGUSR1 \${LINENO} \${__FUNCTION}' USR1
trap 'GENERAL_SIGNAL_HANDLER SIGUSR2 \${LINENO} \${__FUNCTION}' USR2
"
# alias to reset all traps to the defaults
#
alias __unsettraps="
trap - 1
trap - 2
trap - 3
trap - 15
trap - USR1
trap - USR2
"
# -----------------------------------------------------------------------------
#### ----------------
#### ##### general hints
####
#### Do not use variable names beginning with __ (these are reserved for
#### internal use)
####
# save the language setting and switch the language temporary to C
#
__SAVE_LANG="${LANG}"
LANG=C
export LANG
# -----------------------------------------------------------------------------
#### ##### constants
####
#### __TRUE - true (0)
#### __FALSE - false (1)
####
####
typeset -r __TRUE=0
typeset -r __FALSE=1
# -----------------------------------------------------------------------------
#### __KSH_VERSION - ksh version (either 88 or 93)
#### If the script is not executed by ksh the shell is compatible to
### ksh version $__KSH_VERSION
####
__KSH_VERSION=88 ; f() { typeset __KSH_VERSION=93 ; } ; f ;
# use ksh93 features?
#
if [ "${__KSH_VERSION}"x = "93"x ] ; then
__USE_ONLY_KSH88_FEATURES=${__USE_ONLY_KSH88_FEATURES:=${__FALSE}}
else
__USE_ONLY_KSH88_FEATURES=${__USE_ONLY_KSH88_FEATURES:=${__TRUE}}
fi
#### __OS - Operating system (e.g. SunOS)
####
__OS="$( uname -s )"
# ----------------------------------------------------------------------
# read the hash tag of the script
#
#### __SHEBANG - shebank of the script
#### __SCRIPT_SHELL - shell in the shebank of the script
#### __SCRIPT_SHELL_OPTIONS - shell options in the shebank of the script
####
####
__SHEBANG="$( head -1 $0 )"
__SCRIPT_SHELL="${__SHEBANG#*!}"
__SCRIPT_SHELL="${__SCRIPT_SHELL% *}"
__SCRIPT_SHELL_OPTIONS="${__SHEBANG#* }"
[ "${__SCRIPT_SHELL_OPTIONS}"x = "${__SHEBANG}"x ] && __SCRIPT_SHELL_OPTIONS=""
# -----------------------------------------------------------------------------
# specific settings for the various operating systems and shells
#
#
case ${__OS} in
CYGWIN* )
set +o noclobber
__SHELL_FIELD=9
;;
SunOS | AIX )
__SHELL_FIELD=9
;;
* )
__SHELL_FIELD=8
;;
esac
# -----------------------------------------------------------------------------
# specific settings for various shells
#
#### __SHELL - name of the current shell executing this script
####
__SHELL="$( ps -f -p $$ | grep -v PID | tr -s " " | cut -f${__SHELL_FIELD} -d " " )"
__SHELL=${__SHELL##*/}
: ${__SHELL:=ksh}
case "${__SHELL}" in
"bash" )
# set shell options for alias expanding if running in bash
shopt -s expand_aliases
;;
esac
# -----------------------------------------------------------------------------
# define whence if necessary
#
# old definition for whence:
#
# whence whence 2>/dev/null 1>/dev/null || function whence { ksh whence -p $* ; }
# new definition for whence:
#
whence whence 2>/dev/null 1>/dev/null || function whence {
typeset __FUNCTION="whence"; ${__FUNCTION_INIT} ; ${__DEBUG_CODE}
typeset THISRC=1
if typeset -f $1 1>/dev/null ; then
echo $1 ; THISRC=0
elif alias $1 2>/dev/null 1>/dev/null ; then
echo $1 ; THISRC=0
else
which $1 2>/dev/null ; THISRC=$?
fi
${__FUNCTION_EXIT}
return ${THISRC}
}
#### --------------------------------------------------------------------------
#### internal variables
# -----------------------------------------------------------------------------
####
#### __LOG_DEBUG_MESSAGES - log debug messages if set to true
#### This can be activated with the parameter -D msg
####
__LOG_DEBUG_MESSAGES=${__FALSE}
# -----------------------------------------------------------------------------
####
#### __DEBUG_SHELL_IN_ASKUSER - enable or disable the debug shell in AskUser
####
__DEBUG_SHELL_IN_ASKUSER=${__TRUE}
# -----------------------------------------------------------------------------
#### __ACTIVATE_TRACE - log trace messages (set -x) if set to true
#### This can be activated with the parameter -D trace
###
__ACTIVATE_TRACE=${__FALSE}
# -----------------------------------------------------------------------------
####
#### __TRAP_SIGNAL - current trap caught by the trap handler
#### This is a global variable that can be used in the exit routines
####
__TRAP_SIGNAL=""
# -----------------------------------------------------------------------------
#### __USE_RBAC - set this variable to ${__TRUE} to execute this script
#### with RBAC
#### default is ${__FALSE}
####
#### Note: You can also set this environment variable before starting the script
####
: ${__USE_RBAC:=${__FALSE}}
# -----------------------------------------------------------------------------
#### __RBAC_BINARY - pfexec binary
####
#### default is /usr/bin/pfexec
####
#### Note: You can also set this environment variable before starting the script
####
: ${__RBAC_BINARY:=/usr/bin/pfexec}
# -----------------------------------------------------------------------------
#
# user executing this script (works only if using a ssh session with specific
# ssh versions that export these variables!)
#
SCRIPT_USER="$( echo $SSH_ORIGINAL_USER | tr "=" " " | cut -f 5 -d " " )"
SCRIPT_USER_MSG="${SCRIPT_USER}"
# -----------------------------------------------------------------------------
#### __TEE_OUTPUT_FILE - name of the output file if called with the parameter -T
#### default: /var/tmp/$( basename $0 ).$$.tee.log
####
#### Note: You can also set this environment variable before starting the script
####
: ${__TEE_OUTPUT_FILE:=/var/tmp/${0##*/}.$$.tee.log}
# -----------------------------------------------------------------------------
# process the parameter -q or --quiet
#
if [[ \ $*\ == *\ -q* || \ $*\ == *\ --quiet\ * ]] ; then
__NO_HEADERS=${__TRUE}
__QUIET_MODE=${__TRUE}
fi
# -----------------------------------------------------------------------------
# config file found or not
#
__CONFIG_FILE_FOUND=""
# -----------------------------------------------------------------------------
# use the parameter -T or --tee to automatically call the script and pipe
# all output to tee
if [ "${__PPID}"x = ""x ] ; then
__PPID=$PPID ; export __PPID
if [[ \ $*\ == *\ -T* || \ $*\ == *\ --tee\ * ]] ; then
echo "Saving STDOUT and STDERR to \"${__TEE_OUTPUT_FILE}\" ..."
exec $0 $@ 2>&1 | tee -a "${__TEE_OUTPUT_FILE}"
__MAINRC=$?
echo "STDOUT and STDERR saved in \"${__TEE_OUTPUT_FILE}\"."
exit ${__MAINRC}
fi
fi
: ${__PPID:=$PPID} ; export __PPID
# -----------------------------------------------------------------------------
#
# Set the variable ${__USE_RBAC} to ${__TRUE} to activate RBAC support
#
# Allow the use of RBAC to control who can access this script. Useful for
# administrators without root permissions
#
if [ "${__USE_RBAC}" = "${__TRUE}" ] ; then
if [ "$_" != "${__RBAC_BINARY}" -a -x "${__RBAC_BINARY}" ]; then
__USE_RBAC=${__FALSE} "${__RBAC_BINARY}" $0 $*
exit $?
else
echo "${0%%*/} ERROR: \"${__RBAC_BINARY}\" not found or not executable!" >&2
exit 238
fi
fi
# -----------------------------------------------------------------------------
####
#### ##### defined variables that may be changed