piatok 30. októbra 2020

HTTP(s) checks with httpstat.us & curl

Useful site for HTTP(S) testing: https://httpstat.us/

Example 1:

curl --connect-timeout 5 -m 10 https://httpstat.us/200?sleep=7000 > test.curl

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100     6    0     6    0     0      0      0 --:--:--  0:00:07 --:--:--     1

echo $?

0

Example 2:

curl --connect-timeout 5 -m 10 https://httpstat.us/200?sleep=10000 > test.curl

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0

curl: (28) Operation timed out after 10001 milliseconds with 0 out of -1 bytes received

echo $?

28

Notes about timeouts in curl:

https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout/94612

štvrtok 29. októbra 2020

Managing log rotation in Linux

 Add new file to rotation:

  • copy one of /etc/logrotate.d/* configuration files to working (e.g. home) directory, name it e.g. new-file
  • vim new-file
    • change path to point to new (existing) file to be rotated
    • change other parameters, if needed
  • sudo mv new-file /etc/logrotate.d/
  • sudo chown root:root /etc/logrotate.d/new-file
Dry run:

logrotate -d /etc/logrotate.d/new-file

Forced rotation:

sudo logrotate -vf /etc/logrotate.d/new-file

More info:

pondelok 26. októbra 2020

Bash tips I

Inputs to functions:

https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

Outputs from functions:

https://www.linuxjournal.com/content/return-values-bash-functions

Check for empty variable:

https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/

Keyboard shortcuts:

http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/

streda 14. októbra 2020

Statistical analysis of MySQL/MariaDB slow queries

One-liner:

sudo grep -e '^select' -e '^insert' -e '^update' -e '^delete' /srv/data/mysql/mysql_slow.log | cut -d ' ' -f 1-3 | sort | uniq -c | sort -nr | head

utorok 13. októbra 2020

How to extract raw value from JSON and cut it into pieces by size

Sample data:

{
  "var_name1": {},
  "var_name2": "string",
  "var_name3": "long string",
  "var_name4": 15
}

Fill Bash variable with extracted long string raw value from JSON:

VAR_NAME=`cat file.json | jq --raw-output '.var_name3'`

Then reference "chunks" of data using Bash variable index modifiers, e.g. by 255 characters:

  • ${VAR_NAME:0:255}
  • ${VAR_NAME:255:255}
  • ${VAR_NAME:510:255}
  • ${VAR_NAME:765:255}
  • ${VAR_NAME:1020:255}
One-liner alternative extracting and displaying output in single command line:

VAR_NAME=`cat file.json  | jq --raw-output '.var_name3'` ; echo $VAR_NAME ; echo ${VAR_NAME:0:255} ; echo ${VAR_NAME:255:255} ; echo ${VAR_NAME:510:255} ; echo ${VAR_NAME:765:255} ; echo ${VAR_NAME:1020:255}