#210746 - 2015-09-16 06:28 PM
Re: Creating variable name from a variable
[Re: Kevin_J_Murphy]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4563
Loc: USA
|
Is there a reason you need to do this? Just seems overly complicated. Maybe you could explain what you are trying to accomplish and we can show you a better way.
Anyway...
$ServerName="Server01"
$RC=execute("$" + $servername + "=" + $servername )
? $server01
|
|
Top
|
|
|
|
#210750 - 2015-09-17 03:12 PM
Re: Creating variable name from a variable
[Re: Allen]
|
Kevin_J_Murphy
Just in Town
Registered: 2015-09-16
Posts: 4
Loc: NH
|
1st of all, I'm not a programmer 
FWIW( I like the snip above and don't think that's complicated at all)
I have a simple script that reads a text file with server names in it. The script pings each server and displays each server name with UP or DOWN for each as appropriate. It pauses 30 seconds and cycles the list again.
I use it to monitor them while being rebooted during patching. Keeping visual track of each of them showing UP, then DOWN, the UP again is easy enough with a small list, and harder with larger ones.
I want to introduce a "Phase" to it where the initial UP is considered Phase1, the DOWN is Phase2 (during rebooting), then the final UP would be Phase3.
So, to me that means creating a variable for each server with this Phase setting that would be incremented as the server when UP then DOWN then back UP again. Since the list is always different then I was hoping to create a couple variables such as:
$Phase To track the server's phase (i.e. 1 2 or 3) $Status To capture the current UP or DOWN for incrementing Phase#
= the server name in the list currently being processed.
Right now I get output like this: UP (or down)
I'd like to have add the phase so that I can tell if it ever went down or not (and back up again): UP Phase_1 (or _2 or _3 as detected)
|
|
Top
|
|
|
|
#210755 - 2015-09-17 05:06 PM
Re: Creating variable name from a variable
[Re: Kevin_J_Murphy]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
Yikes! Using per-host vars is complicated! Using some UDFs:
; load external functions via call if you don't copy/paste the content directly into this script
Call '.\FileIO.kxf'
Call '.\Ping.kxf'
; clear DONE flag
$Done = 0
; load list of monitored devices into an array
$aTargets = FileIO('.\ServerList.txt', 'R')
; Create a status array to match the targets
Dim $aStatus[UBound($aTargets)]
; Initialize the status array to Status:1
For $ = 0 to UBound($aStatus)
$aStatus[$P] = 1
Next
; loop until all devices are at Status:3
While Not $Done
Cls
$Done = 1 ; assume complete
; enumerate the list of targets
For $P = 0 to UBound($aTargets)
If Ping($Target) ; host is up - responds to Ping
If $aStatus[$P] = 2 ; prior status was down
$aStatus[$P] = 3 ; log the transition from down to up (Status:2 to Status:3)
EndIf
If $aStatus[$P] = 1 ; not rebooted yet
$Done = 0 ; this host hasn't rebooted, we're not done
EndIf
Else
$aStatus[$P] = 2 ; Host is down - set Status:2
$Done = 0 ; this host is (still) down, we're not done
EndIf
$Target ' is at Stage: ' $aStatus[$P] @CRLF
Next
Sleep 60 ; wait 60 seconds and do it all again
Loop
'All hosts have completed the reboot process.' @CRLF This is untested, and you should declare the vars, but basically, it loads a list of hostnames into an array. It creates a duplicate array to hold the per-host status, setting it to "1". It pings each host. If the host responds AND the status is 1, it does nothing but clear the DONE flag. If the host responds AND the status is 2, it moves to Stage:3 - reboot complete. If the host doesn't respond, it sets the Stage to 2 (down) and clears the DONE flag.
As long as the DONE flag is 0, the loop repeats the process (after the delay). When all hosts have rebooted and come back up (transitioned from stage 2 to 3), the DONE flag will not be cleared and the loop will exit.
You might want to add a test to insure that the process only runs 60 times (one hour, assuming a 60-second delay between loops). If you want this, just add a $L value, increment it prior to the Loop statement, and add If $L > 60 $Done = 1 EndIf.
In this case, there is never a transition from stage 3 back to 1. The program ends when all monitored systems have rebooted. Running it again starts from state 1.
You will need to add / modify the status messages as your requirements dictate.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1022 anonymous users online.
|
|
|