Monitoring Windows Servers through script - scripting

I need to monitor the CPU and the memory usage in windows servers when executing performance tests on them but I need them to be via script.
In Unix systems, I have the scripts and, for example, to monitor the cpu, I use this line:
cpuPerc=$(top -n 1 -b|grep Cpu|awk '{print $2}'|cut -d"%" -f1"")
And then, I send it to a file or to terminal with an echo.
Thanks.

If you want to do it with powershell you can see an simple example here : http://technet.microsoft.com/en-us/magazine/ee872428.aspx
If you are on a localised windows (not english), you will have to get the correct counter name before.
For example in my french windows version it will be :
Get-counter -ListSet *
this will display all the available counterset. I can see there is a set named "processeur" wich is the french word for CPU. So now i can get the available counter for this set like this:
Get-counter -ListSet "processeur" |select -expand counter
it gives me the result :
PS>Get-counter -ListSet "processeur" |select -expand counter
\Processeur(*)\% temps processeur
\Processeur(*)\% temps utilisateur
\Processeur(*)\% temps privilégié
\Processeur(*)\Interruptions/s
\Processeur(*)\% temps DPC
\Processeur(*)\% temps d'interruption
\Processeur(*)\DPC mis en file d'attente/s
\Processeur(*)\Taux DPC
\Processeur(*)\% d'inactivité
\Processeur(*)\% durée C1
\Processeur(*)\% durée C2
\Processeur(*)\% durée C3
\Processeur(*)\Transitions C1/s
\Processeur(*)\Transitions C2/s
\Processeur(*)\Transitions C3/s
Now I can use one or more of these counter like this :
get all the counters : Get-counter -ListSet "processeur" |select -expand counter | foreach{ get-counter $_ -SampleInterval 2 -MaxSamples 10}
get inactivity counter : Get-counter -ListSet "processeur" |select -expand counter |where {$_ -match "inactivité"} | foreach{ get-counter $_ -SampleInterval 2 -MaxSamples 10}
At least, you can pipe the result to export-counter in order to save it to a file (CSV, TSV, or BLG).
Hope that's help

Related

How to use text file as input to feed in the interactive input of smalltalk and redirect output to a file

I am struggling to find out is there a way to feed input
to the interactive command of gst a.st b.st ... -
and redirect the output. Normally, the interactive buffer will
have st> ... and when you type a command it will output something by calling
the default/override displayString method to the interactive output. How to get the input
and feed the output using linux command or maybe a tiny smalltalk test script to do that.
Thank you.
Here's a contrived demonstration program. It reads in strings from standard input until EOF, sorts them, then prints them out:
input := stdin nextLine.
c := OrderedCollection new.
[ input ~= nil ] whileTrue: [
c add: input.
input := stdin nextLine.
].
c sort do: [ :each | each printNl ]
You can run it interactively (pressed Ctrl-D after entering hhh):
$ gst sortprog.st
tttt
aaa
vvvv
hhh
'aaa'
'hhh'
'tttt'
'vvvv'
Or I can create a text file test.in with the following contents:
tttt
aaa
vvvv
hhh
Then run:
$ gst sortprog.st < test.in > test.out
And then check the contents of the output file:
$ cat test.out
'aaa'
'hhh'
'tttt'
'vvvv'
If your program has prompts, they will appear in the output file, of course. Anything going to stdout will go to that file.

Get signal quality in % from iwconfig

first post for help....
I am trying to get signal quality in % from the iwconfig command...
I began with this command but it returns the text 'signal value'
iwconfig wlan0 | grep Quality | awk -F '=|/|=[ ]' '{print $2,$3}'
70 70 Signal level
I'd like just want to have '100%' as a result
wlan0 IEEE 802.11 ESSID:"SKYNET"
Mode:Managed Frequency:2.412 GHz Access Point: AC:84:C9:CD:79:E0
Bit Rate=72.2 Mb/s Tx-Power=31 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=68/70 Signal level=-42 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:51 Invalid misc:0 Missed beacon:0
Following is doing (68/70)*100. using split() 68 is stored in a[2] whereas 70 is stored in a[3]
iwconfig wlan0|awk '/Link Quality/{split($2,a,"=|/");print (a[2]/a[3])*100"%"}'
97.1429%
for integer result:
iwconfig wlan0|awk '/Link Quality/{split($2,a,"=|/");print int((a[2]/a[3])*100)"%"}'
97%
or just use printf:
iwconfig wlan0|awk '/Link Quality/{split($2,a,"=|/");printf "%d%s\n", (a[2]/a[3])*100, "%"}'
97%
PS: Tested with the output supplied by OP in question.

Setting DTR high, RTS low using Linux bash?

I have a serial device that has no flow control, but is powered from the RS232 port by holding the RTS high and DTR low
I was hoping to read from this device using a simple bash script, but can't find any way to set the handshaking lines, using stty or otherwise, to allow for the above configuration.
Any ideas if this is possible?
I don't have an answer about setting RTS without touching DTR, because I don't have any DTR pin on my dongle; but, trying to set RTS was already very tricky un pure shell.
You may need to play with stty crtscts and clocal flags.
I have published a detailed answer here:
https://forums.gentoo.org/viewtopic-p-8132756.html#8132756
Here is the short version:
#!/bin/bash
MySerialPort="/dev/ttyUSB0"
MyLatency="2"
echo "#include <fcntl.h>
#include <sys/ioctl.h>
main()
{ int fd; fd = open(\"${MySerialPort}\",O_RDWR | O_NOCTTY );
int RTS_flag; RTS_flag = TIOCM_RTS;
ioctl(fd,TIOCMBIS,&RTS_flag);
sleep (${MyLatency});
ioctl(fd,TIOCMBIC,&RTS_flag);
close(fd); } " | tcc -run -
Note that sending data on TX will probably mess RTS; see the Gentoo forum for details.
I've been trying something similar here.
I used the ioctls. Then measured with a multimeter.
this is what I found:
dsrv=TIOCM_DTR;//this sets RTS to -11.7?
dsrv=TIOCM_RTS;//sets DTR -11.7
ioctl(fd, TIOCMBIS, &dsrv);
dsrv=TIOCM_DTR;//this sets RTS to 11.7?
dsrv=TIOCM_RTS;//sets DTR 11.7
ioctl(fd, TIOCMBIC, &dsrv);
It is somewhat weird...
socat controls DTR when hucpcl=1
(Atmel EDBG USB Serial needs DTR high)
sleep 1; while true; do echo -en "\x01\x02"; sleep 0.1; done | socat -T1 -t1 - /dev/ttyUSB0,hupcl=1,raw,b1000000,cs8,echo=0
man socat - may be find more solutions for your serial problem.

CC and SSN File Search on Network Drives

I am working on a project where we need to search a set of our network drives to examine each file and look for credit card numbers and social security numbers. I have been trying to use the Cornell Spider program without success since it seems to crash every time I use it.
I would like to know if there is a way to use Powershell, or a scripting language available on Windows, to perform an analysis (I am assuming a strings match) that would match the patterns for credit card numbers and social security numbers (probably a regex). If there is a way, and since I am not a programmer, I was curious if there was some code that I could use to do this with. Also, the ability to save/dump the results of what is found out to a file (text or CSV) would be very helpful as well.
Any ideas or help that you can provide would be greatly appreciated.
=======================================================
Okay, I have been working on a test script and have come up with the following:
$spath = "C:\Users\name\Desktop\"
$opath = "C:\Users\name\Desktop\Results.txt"
$Old_SSN_Regex = "[0-9]{3}[-| ][0-9]{2}[-| ][0-9]{4}"
$SSN_Regex = "^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$"
$CC_Regex = "^((?:4\d{3})|(?:5[1-5]\d{2})|(?:6011)|(?:3[68]\d{2})|(?:30[012345]\d))[ -]?(\d{4})[ -]?(\d{4})[ -]?(\d{4}|3[4,7]\d{13})$"
$CC_2_Regex = "^(\d{4}-){3}\d{4}$|^(\d{4} ){3}\d{4}$|^\d{16}$"
Get-ChildItem $spath -Include *.txt -Recurse | Select-String -Pattern $SSN_Regex | Select-Object Path,Filename,Matches | Out-File $opath
Get-ChildItem $spath -Include *.txt -Recurse | Select-String -Pattern $CC_Regex | Select-Object Path,Filename,Matches | Out-File $opath -Append
Get-ChildItem $spath -Include *.txt -Recurse | Select-String -Pattern $CC_2_Regex | Select-Object Path,Filename,Matches | Out-File $opath -Append
This seems to be working well, the problem is that if there is a space before or after the item to be matched, the regexs listed do not catch it. Is there something that I can do differently so that it will catch the item if it has a space before or after the pattern to be matched within a file?
See this PowerGUI.org thread for the solution: PowerShell Script to locate Social Security Numbers (SSN) and Credit Card numbers in files across the network.

Using Squeak from a shell

Can I launch Squeak as a REPL (no GUI), where I can enter and evaluate Smalltalk expressions? I know the default image don't allow this. Is there any documentation on how to build a minimum image that can be accessed from a command-line shell?
Here is a (hackish) solution:
First, you need OSProcess, so run this in a Workspace:
Gofer new squeaksource:'OSProcess'; package:'OSProcess';load.
Next, put this in the file repl.st:
OSProcess thisOSProcess stdOut
nextPutAll: 'Welcome to the simple Smalltalk REPL';
nextPut: Character lf; nextPut: $>; flush.
[ |input|
[ input := OSProcess readFromStdIn.
input size > 0 ifTrue: [
OSProcess thisOSProcess stdOut
nextPutAll: ((Compiler evaluate: input) asString;
nextPut: Character lf; nextPut: $>; flush
]
] repeat.
]forkAt: (Processor userBackgroundPriority)
And last, run this command:
squeak -headless path/to/squeak.image /absolute/path/to/repl.st
You can now have fun with a Smalltalk REPL. Dont forget to type in the command:
Smalltalk snapshot:true andQuit:true
if you want to save your changes.
Now, onto the explanation of this solution:
OSProcess is a package that allows to run other processes, read from stdin, and write to stdout and stderr. You can access the stdout AttachableFileStream with OSProcess thisOSProcess (the current process, aka squeak).
Next, you run an infinite loop at userBackgroundPriority (to let other processes run). In this infinite loop, you use Compiler evaluate: to execute the input.
And you run this in a script with a headless image.
As of Pharo 2.0 (and 1.3/1.4 with the fix described below), there are no more hacks necessary. The following snippet will turn your vanilla Pharo image into a REPL server...
From https://gist.github.com/2604215:
"Works out of the box in Pharo 2.0. For prior versions (definitely works in 1.3 and 1.4), first file in https://gist.github.com/2602113"
| command |
[
command := FileStream stdin nextLine.
command ~= 'exit' ] whileTrue: [ | result |
result := Compiler evaluate: command.
FileStream stdout nextPutAll: result asString; lf ].
Smalltalk snapshot: false andQuit: true.
If you want the image to always be a REPL, put the code in a #startup: method; otherwise, pass the script at the command line when you want REPL mode, like:
"/path/to/vm" -headless "/path/to/Pharo-2.0.image" "/path/to/gistfile1.st"
Please visit:
http://map.squeak.org/package/2c3b916b-75e2-455b-b25d-eba1bbc94b84
and Run Smalltalk on server without GUI?
The project http://www.squeaksource.com/SecureSqueak.html includes a REPL package that may provide much of what you are looking for.