piątek, 31 marca 2017

python tricks - “_” operator.

t’s a useful feature which not many of us are aware.
In the Python console, whenever we test an expression or call a function, the result dispatches to a temporary name, _ (an underscore).


>>> 2 + 1
3
>>> print _
3
>>>

The “_” references to the output of the last executed expression.

czwartek, 23 marca 2017

check state of all docker instances

for((j=1;j<7;j++)); do for i in 9990 10090 10190; do echo -n "app0$j on port $i" && curl  --digest http://admin:pass@app0$j.prod.crm.polsatc:$i/management --header "Content-Type: application/json" -d '{"operation":"read-attribute","name":"server-state","json.pretty":1}'; done; done


desc:
make loop 7 times
make loop on 3 port numbers

paste two variables to http address and get data via docker api


10 largest file

find . -mount -type f -ls 2> /dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'

        20 MB   ./kontvmq19_1.1.log
         6 MB   ./kontvmq19_1.0.log
         0 MB   ./flopsar-agent-2.0.jar
         0 MB   ./kontwaw5.0.log
         0 MB   ./kontvmq19_1.0.log.lck

create menu bash

#!/bin/bash
show_menu()
{

        echo "what we can do: "
        echo "1) find oom"
        echo "2) delete oom"
        echo "3) check wf"
        echo "4) exit"
        echo "-=-=-=-=-=-=-=-=-=-"
        echo -n
        echo -n
}

read_options(){
        local choice
        read -p "enter choice [1 - 4] " choice
        case $choice in
                1)  for i in {1..7}; do ssh ansible@app0$i.prod.crm.polsatc 'bash -s' < oom.sh ;done ;;
                2)  for i in {1..7}; do ssh ansible@app0$i.prod.crm.polsatc 'bash -s' < eraseHprof.sh ;done ;;
                3)  for i in {1..7}; do ssh ansible@app0$i.prod.crm.polsatc 'bash -s' < wf.sh ;done ;;
                4)  exit 0;;
                *) echo -e "${RED}ERROR...${STD}" && sleep 2
        esac
}

while true
do
        show_menu
        read_options
done

wtorek, 21 marca 2017

detailed web response time

marek@bunny:~$ curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://www.blogosit.blogspot.com

Lookup time:    0.125
Connect time:   0.137
PreXfer time:   0.137
StartXfer time: 0.269

Total time:     0.269

security - auth.log parse

how to check auth.log easly

marek@bunny:~$ uptime
 20:44:25 up  6:08,  2 users,  load average: 0.00, 0.00, 0.00


marek@bunny:~$ for i in `sudo grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" /var/log/auth.log  | sort | uniq | grep -v 0.0.0.0`; do geoiplookup $i; done

GeoIP Country Edition: VN, Vietnam
GeoIP Country Edition: US, United States
GeoIP Country Edition: DZ, Algeria
GeoIP Country Edition: MU, Mauritius
GeoIP Country Edition: DZ, Algeria
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: KR, Korea, Republic of
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: BR, Brazil
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: IR, Iran, Islamic Republic of
GeoIP Country Edition: EG, Egypt
GeoIP Country Edition: IT, Italy
GeoIP Country Edition: IP Address not found
GeoIP Country Edition: US, United States
GeoIP Country Edition: TR, Turkey
GeoIP Country Edition: ES, Spain
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: CN, China
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: IR, Iran, Islamic Republic of
GeoIP Country Edition: RO, Romania
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: PE, Peru
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: UA, Ukraine
GeoIP Country Edition: GB, United Kingdom
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: MA, Morocco
GeoIP Country Edition: CL, Chile
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: AR, Argentina
GeoIP Country Edition: IT, Italy
GeoIP Country Edition: PL, Poland
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: PL, Poland
GeoIP Country Edition: US, United States
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: CN, China
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: CN, China
GeoIP Country Edition: IT, Italy
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: RU, Russian Federation
GeoIP Country Edition: CZ, Czech Republic
GeoIP Country Edition: RU, Russian Federation


how to count this sinners?


marek@bunny:~$ cat ip_tst.txt | sort | uniq | xargs -n 1 geoiplookup { } | sort | uniq -c | sort
     12 GeoIP Country Edition: AR, Argentina
      1 GeoIP Country Edition: BR, Brazil
      1 GeoIP Country Edition: CL, Chile
      1 GeoIP Country Edition: CZ, Czech Republic
      1 GeoIP Country Edition: EG, Egypt
      1 GeoIP Country Edition: ES, Spain
      1 GeoIP Country Edition: GB, United Kingdom
      1 GeoIP Country Edition: IP Address not found
      1 GeoIP Country Edition: KR, Korea, Republic of
      1 GeoIP Country Edition: MA, Morocco
      1 GeoIP Country Edition: MU, Mauritius
      1 GeoIP Country Edition: PE, Peru
      1 GeoIP Country Edition: RO, Romania
      1 GeoIP Country Edition: TR, Turkey
      1 GeoIP Country Edition: UA, Ukraine
      1 GeoIP Country Edition: VN, Vietnam
     27 GeoIP Country Edition: CN, China
      2 GeoIP Country Edition: DZ, Algeria
      2 GeoIP Country Edition: IR, Iran, Islamic Republic of
      2 GeoIP Country Edition: PL, Poland
      3 GeoIP Country Edition: IT, Italy
      3 GeoIP Country Edition: US, United States
      9 GeoIP Country Edition: RU, Russian Federation


sobota, 11 marca 2017

delete first line from command output

ubuntu@ip-172-31-44-77:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jboss/wildfly       latest              27e70d979161        12 weeks ago        582.8 MB

ubuntu@ip-172-31-44-77:~$ sudo docker images | awk '{if(NR>1)print}'
jboss/wildfly       latest              27e70d979161        12 weeks ago        582.8 MB

czwartek, 9 marca 2017

swap consuming process - bash script

SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d -regex "^/proc/[0-9]+"`
do
    PID=`echo $DIR | cut -d / -f 3`
    PROGNAME=`ps -p $PID -o comm --no-headers`
    for SWAP in `grep VmSwap $DIR/status 2>/dev/null | awk '{ print $2 }'`
    do
        let SUM=$SUM+$SWAP
    done
    if (( $SUM > 0 )); then
        echo "PID=$PID swapped $SUM KB ($PROGNAME)"
    fi
    let OVERALL=$OVERALL+$SUM
    SUM=0
done
echo "Overall swap used: $OVERALL KB"

środa, 8 marca 2017

winrm python

import winrm

s = winrm.Session('kontvmq08', auth=('test', 'Redhat2017'))
r = s.run_cmd('net stop K_KIL_BIL')
print(r.status_code)
print(r.std_out)

necessary configuration:

winrm quickonfig
Test-WSMan -ComputerName <name>

winrm set winrm/config/service @{AllowUnencrypted="true"}
winrm set winrm/config/service/auth @{Basic="true"}
winrm set winrm/config/client/auth @{Basic="true"}


necessary module:

pip install "pywinrm>=0.2.2"

poniedziałek, 6 marca 2017

Something new in Docker / redhat


docker@gitvmd2:~$ docker --version
Docker version 1.13.1, build 092cba3
docker@gitvmd2:~$ docker system

Usage:  docker system COMMAND

Manage Docker

Options:
      --help   Print usage

Commands:
  df          Show docker disk usage
  events      Get real time events from the server
  info        Display system-wide information
  prune       Remove unused data

Run 'docker system COMMAND --help' for more information on a command.