0

Linux novice here so bear with me here.

I am writing a Bash Script for school (On a CentOS 8 VM) and I am attempting to save the output of Siege (Load Tester) to a variable so I can compare values.

Here is the issue I am running into: The HTTP lines between "The server is now under siege" and "Lifting the server siege..." are what are being stored in the variable, and not the nice little summary after "Lifting the server siege..."

[root@prodserver siege-4.1.1]# siege -c 1 -t 1s 192.168.1.3
** SIEGE 4.1.1
** Preparing 1 concurrent users for battle.
The server is now under siege...
HTTP/1.1 200     0.00 secs:    6707 bytes ==> GET  /
HTTP/1.1 200     0.01 secs:    2008 bytes ==> GET  /assets/images/taste_bug.gif
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
...Shortened for readability...
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
HTTP/1.1 200     0.01 secs:    2008 bytes ==> GET  /assets/images/taste_bug.gif
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif

Lifting the server siege...
Transactions:                149 hits
Availability:             100.00 %
Elapsed time:               0.22 secs
Data transferred:           3.95 MB
Response time:              0.00 secs
Transaction rate:         677.27 trans/sec
Throughput:            17.97 MB/sec
Concurrency:                1.00
Successful transactions:         149
Failed transactions:               0
Longest transaction:            0.01
Shortest transaction:           0.00

Currently this is how I am attempting to store the variable in bash:

SIEGE="$(siege -c $1 -t $2 [ip])"

As mentioned before, when I echo $SIEGE, it turns out the variable stored all the HTTP lines and NOT the Summary after "Lifting the siege..."

My question is how can I store that Summary in a variable.

jxf6947
  • 1
  • 1
  • Do not see command which produces first output. Also it is not clear what output is produced by your "siege -c $1 -t $2 [ip]" command and how you created this output which you want to save to the variable. – Saboteur Sep 25 '21 at 20:41
  • 1
    do not use lnks to external images but instead cut-n-paste the sample output directly into the question (apply code formatting as needed), otherwise if/when the linked image disappears we're left with an incomplete question; as for capturing the `siege` output ... seeing how it's quite large ... I'd recommend saving to a file and then parsing the file (it's much easier to parse a file than a variable that's been stuffed with a lot of lines of text) – markp-fuso Sep 25 '21 at 20:42
  • @Saboteur Updated for clarity. At the time I wasnt able to get the output from the VM to my desktop, but I found a way. I provided the output of the command, and clarified the question. – jxf6947 Sep 25 '21 at 22:38
  • @markp-fuso Updated for clarity. At the time I wasnt able to get the output from the VM to my desktop, but I found a way. I provided the output of the command, and clarified the question. – jxf6947 Sep 25 '21 at 22:38

1 Answers1

1

NOTE: I'm not familiar with siege so I have no idea if all of the output is going to stdout or if some of the output could be going to stderr.

Assuming all siege output is going to stdout ... a couple ideas depending on which lines need to be ignored:

# grab lines from `^Lifting` to end of output:

SIEGE="$(siege -c $1 -t $2 [ip] | sed -n '/^Lifting/,$ p')"

# ignore all lines starting with `^HTTP`

SIEGE="$(siege -c $1 -t $2 [ip] | grep -v '^HTTP')"

If it turns out some of the output is being sent to stderr, change the siege call to redirect stderr to stdout:

# from

siege -c $1 -t $2 [ip]

# to

siege -c $1 -t $2 [ip] 2>&1

Though I'd probably opt for saving all output to a file and then parsing the file as needed, ymmv ...

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • Thank you for your help. Sadly none of those seemed to get me where I wanted. I tried saving the output to a text file, however the final summary the command gives off was not included in the text file. Im wondering if its even possible. Almost seems like its a script of its own that runs once its finished. – jxf6947 Sep 28 '21 at 13:55
  • if adding `2>&1` (redirect stderr to stdout) did not work ... *sorry* ... would need more details on what `siege` is and how it generates output (eg, as you mentioned, perhaps a separate OS process that writes to the same tty ... ?) – markp-fuso Sep 28 '21 at 14:00