Pinpoint data in line

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Pinpoint data in line

22 Oct 2018, 12:52

Hello all,

I am trying to get my head around this, but can't get there..
I need to extract values from this txt i have, called "trimballs.txt"
The file contains a dump of the "df" bash command on a NAS, and i already trimmed down the file.
Now, im about to make a GUI for the thing, to make it always available.
Thing is, i need to extract the percentage of occupation of the drives......

This is the file:

Code: Select all

/dev/sda1      2883220084 2802107688   81096012  98% /srv/dev-disk-by-label-RED1
/dev/sdb1      1921802500  109131216 1812654900   6% /srv/dev-disk-by-label-MOV1
/dev/sdc1       479669932  102827508  376826040  22% /srv/dev-disk-by-label-BACKUPS
/dev/md0        719897488  409334784  310546320  57% /export/Main
This is the code so far:

Code: Select all

; Get them infos brah
FileDelete, spaceballs.txt
FileDelete, splitballs.txt

Run, plink.exe -ssh -pw "xxxxxxx"  [email protected] df > /srv/dev-disk-by-label-MainMirror/NasX/.spacespecs/spaceballs.txt
sleep 500
FileMove, \\NASBOX\Main\.spacespecs\spaceballs.txt, %A_ScriptDir%
sleep 1000

; Strip yo data bitch!
Trash := "Sist.fichs"
Trash2 := "folder2ram"
Trash3 := "tmpfs"
Trash4 := "udev"
Trash5 := "/dev/sdf1"
FileRead, var, spaceballs.txt
FileDelete, spaceballs.txt
Loop, Parse, var, `n, `r
If not instr(A_LoopField, Trash)
If not instr(A_LoopField, Trash2)
If not instr(A_LoopField, Trash3)	  
If not instr(A_LoopField, Trash4)
If not instr(A_LoopField, Trash5)
New_data .= A_LoopField "`n"
FileAppend, %New_data%, splitballs.txt
New_data := ""


; Gimme your percentagesssss

I know the trash clean routine could be optimized, but ill get to it later...
I just needed some help from you guys in figuring out how to go trough the lines on the txt and getting only the values of disk percentage.

Thanks in advance!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Pinpoint data in line

22 Oct 2018, 13:29

Code: Select all

fakeloopfield := "/dev/sda1      2883220084 2802107688   81096012  98% /srv/dev-disk-by-label-RED1"
RegExMatch(fakeloopfield, "\d+`%", match)
MsgBox % match
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: Pinpoint data in line

22 Oct 2018, 14:09

Well, that works if i put the line on the file manually :D
That i could do aswell on earlier iterations of the script.

I was wondering on how to do it on the whole file.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Pinpoint data in line

22 Oct 2018, 15:18

the line is already "put on the file" programatically by the parsing loop for you. u place the regex inside ur loop and run it on A_LoopField. i assumed fakeloopfield wouldve sufficed as a hint
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: Pinpoint data in line

22 Oct 2018, 21:16

Well, made it a working proggie....

Image

I know it's really dirty... But it's a work in progress.
I know there are apps that do this, but what's the fun in just installing them? :)


Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


;#######################
;            GLOBALS
;#######################

URL=192.168.1.70

NasServer=%URL%




;#######################
;            GET DATA
;#######################

; Starting cleanup
if FileExist("spaceballs.txt")
	FileDelete, spaceballs.txt

if FileExist("splitballs.txt")
	FileDelete, splitballs.txt

Sleep 50

; Get them infos brah
Run, plink.exe -ssh -pw "xxxxxxxxx"  xxxxxxxx@%NasServer% df > /srv/dev-disk-by-label-MainMirror/NasX/.spacespecs/spaceballs.txt, ,Hide
	sleep 500

FileMove, \\NASBOX\Main\.spacespecs\spaceballs.txt, %A_ScriptDir%
	sleep 500





;#######################
;            DATA HANDLE
;#######################

; Strip yo data bitch!


Trash := "Sist.fichs"
Trash2 := "folder2ram"
Trash3 := "tmpfs"
Trash4 := "udev"
Trash5 := "/dev/sdf1"

Sleep 50

FileRead, var, spaceballs.txt
FileDelete, spaceballs.txt

Sleep 50

Loop, Parse, var, `n, `r
		If not instr(A_LoopField, Trash)
		If not instr(A_LoopField, Trash2)
		If not instr(A_LoopField, Trash3)	  
		If not instr(A_LoopField, Trash4)
		If not instr(A_LoopField, Trash5)
	New_data .= A_LoopField "`n"
FileAppend, %New_data%, splitballs.txt
New_data := ""

Sleep 50

FileReadLine, Sda1, splitballs.txt, 1
FileReadLine, Sda2, splitballs.txt, 2
FileReadLine, Sda3, splitballs.txt, 3
FileReadLine, Md0, splitballs.txt, 4

Sleep 50

RegExMatch(Sda1, "\d+`%", Perc1) ; Percentagem  - sda1 (RED1)
RegExMatch(Sda2, "\d+`%", Perc2) ; Percentagem  - sda2 (MOV1)
RegExMatch(Sda3, "\d+`%", Perc3) ; Percentagem  - sda3 (BACKUPS)
RegExMatch(Md0, "\d+`%", Perc4) ; Percentagem  - md0 (Main RAID)

Sleep 50







;#######################
;            GUI WORKS 
;#######################

Gui, +AlwaysOnTop
Gui, font, s8 cBlack
Gui, Show, w200 h270

; GuiBoxes
	Gui,Add, GroupBox, x10 y10 w180 h150, Dados
	Gui,Add, GroupBox, x10 y160 w180 h80, NAS Ping

; Endereço NAS
	Gui, Add, Text, x20 y220 w160 h20 vIpnas, NAS Address: %URL%

; percentagens
	Gui, Add, Text, x20 y30 w30 h20 +BackgroundTrans Right, %Perc1%
	Gui, Add, Text, x20 y60 w30 h20 +BackgroundTrans Right, %Perc2%
	Gui, Add, Text, x20 y90 w30 h20 +BackgroundTrans Right, %Perc3%
	Gui, Add, Text, x20 y120 w30 h20 +BackgroundTrans Right, %Perc4%
; barras
	Gui, Add, Progress, x55 y30 w100 h20  vPerc1 cRed, %Perc1%
	Gui, Add, Progress, x55 y60 w100 h20  vPerc2 cRed, %Perc2%
	Gui, Add, Progress, x55 y90 w100 h20  vPerc3 cRed, %Perc3%
	Gui, Add, Progress, x55 y120 w100 h20  vPerc4 cRed, %Perc4%

; barras ping
	Gui, Add, Progress, x20 y180 w10 h10  vPing1 cGreen Backgroundf0f0f0, 100
	Gui, Add, Progress, x30 y180 w10 h10 vPing2 cGreen Backgroundf0f0f0, 100
	Gui, Add, Progress, x40 y180 w10 h10 vPing3 cYellow Backgroundf0f0f0, 100
	Gui, Add, Progress, x50 y180 w10 h10 vPing4 cRed Backgroundf0f0f0, 100
; Ping response
	Gui, Add, Text, x20 y195 w150 h20 vStatusPing
	
; botões
	Gui, Add, Button,  x10 y240 w70 h25 gGtfo, Sair
	

Sleep 500






;#######################
;            PING PONG
;#######################


Loop:
while(1) {
        Ping := SimplePing(URL)
        GuiControl,, Text, Ping: %Ping%ms
        If (Ping == "No response."){
                GuiControl,, Text, No Response from Server :(
                Ping = 1000
        }
        UpdateBar(Ping)
        comp := URL
        gui, submit, NoHide
        if(URL <> comp)
                GuiControl,, Text, Retrieving Initial Ping...
}
 
 
SimplePing(URL, timeout = 3000)
{
        Runwait,%comspec% /c ping -w %timeout% %url%>ping.log,,hide
        fileread , StrTemp, ping.log
        if RegExMatch(StrTemp, "Average = (\d+)", result)
                speed:=result1
        else
                speed := "No response."
        return speed
        filedelete ping.log
}

Sleep 50

UpdateBar(ping)
{
        If (ping 0)
        {
                GuiControl,, Ping1, %Ping%
                GuiControl,, Ping2, 0
                GuiControl,, Ping3, 0
                GuiControl,, Ping4, 0
				GuiControl,,StatusPing, ONLINE %Ping% ms
                return
        }
        If (ping < 10)
        {
                Ping:=Ping-10
                GuiControl,, Ping1, 100
                GuiControl,, Ping2, %Ping%
                GuiControl,, Ping3, 0
                GuiControl,, Ping4, 0
				GuiControl,,StatusPing, FULL SPEED %Ping%ms
                return
        }      
        If (ping < 30)
        {
                Ping:=Ping-30
                GuiControl,, Ping1, 100
                GuiControl,, Ping2, 100
                GuiControl,, Ping3, %Ping%
                GuiControl,, Ping4, 0
				GuiControl,,StatusPing, SLOW SPEED %Ping%ms
                return
        }      
        If (ping < 50)
        {
                Ping:=Ping-50
                GuiControl,, Ping1, 100
                GuiControl,, Ping2, 100
                GuiControl,, Ping3, 100
                GuiControl,, Ping4, %Ping%
				GuiControl,,StatusPing, PROBLEMS %Ping%ms
        }
        If (ping > 999)
        {
				Ping:=Ping-999
				GuiControl,, Ping1, 100
                GuiControl,, Ping2, 100
                GuiControl,, Ping3, 100
                GuiControl,, Ping4, 100
				GuiControl,,StatusPing, OFFLINE
        }
}

Gui, Show
Gui, +LastFound
Winset, Redraw

return
GuiClose:
GuiEscape:
ExitApp

Gtfo:
ExitApp


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Google [Bot] and 395 guests