Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 26/01/2016, à 23:25

k3c

trier un flux en bash

Bonjour

Comme j'utilise pas mal docker, j'ai une commande que j'aime bien
docker stats
https://docs.docker.com/engine/referenc … ine/stats/

qui affiche des choses du genre

CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
41769443a96d        0.21%               1.753 MB / 8.289 GB   0.02%               6.976 kB / 648 B    2.212 MB / 0 B
60e68fef3354        0.21%               197.8 MB / 8.289 GB   2.39%               0 B / 0 B           191.6 MB / 4.506 MB
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               14.24 kB / 648 B    10.34 MB / 0 B

Par défaut cette commande affiche tous les containers actifs
Je sais récupérer le nombre de containers actifs

$ docker ps -q
60e68fef3354
41769443a96d
7779174e2a53
$ docker ps -q | wc -l
3
$ 

Je voudrais faire un script en bash, qui lise le flux qu'envoie la commande
docker stats
et qui trie, sur n'importe quelle colonne, par exemple, trier par consommation CPU, ascendant ou descendant, ou sur un autre critère.

Merci pour toute idée

Dernière modification par k3c (Le 26/01/2016, à 23:29)


Archlinux sur Xiaomi Air 13

Hors ligne

#2 Le 27/01/2016, à 10:38

credenhill

Re : trier un flux en bash

hello
trie par CPU, champ 2, puis par MEM, champ 3
on extrait la ligne d'entête pour ne pas la trier

$ docker stats | tee >(head -1) > >(tail -n +2 | sort -nk 2); sleep 2 
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               14.24 kB / 648 B    10.34 MB / 0 B
41769443a96d        0.21%               1.753 MB / 8.289 GB   0.02%               6.976 kB / 648 B    2.212 MB / 0 B
60e68fef3354        0.21%               197.8 MB / 8.289 GB   2.39%               0 B / 0 B           191.6 MB / 4.506 MB
$ docker stats| tee >(head -1) > >(tail -n +2 | sort -nk 3); sleep 2 
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
41769443a96d        0.21%               1.753 MB / 8.289 GB   0.02%               6.976 kB / 648 B    2.212 MB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               14.24 kB / 648 B    10.34 MB / 0 B
60e68fef3354        0.21%               197.8 MB / 8.289 GB   2.39%               0 B / 0 B           191.6 MB / 4.506 MB

Hors ligne

#3 Le 27/01/2016, à 10:54

k3c

Re : trier un flux en bash

Bonjour

Merci pour ce script, mais on dirait qu'il ne trie pas correctement chez moi

au départ j'ai

$ docker stats --no-stream
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O               BLOCK I/O
3276efb16bf7        0.00%               0 B / 0 B             0.00%               0 B / 0 B             0 B / 0 B
41769443a96d        0.01%               1.753 MB / 8.289 GB   0.02%               29.22 kB / 648 B      2.212 MB / 0 B
60e68fef3354        0.08%               271.3 MB / 8.289 GB   3.27%               0 B / 0 B             254.7 MB / 39.58 MB
72ebb165029b        0.00%               13.66 MB / 8.289 GB   0.16%               9.036 MB / 7.427 MB   458.8 kB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               36.38 kB / 648 B      10.34 MB / 0 B
e0e2560f1992        0.00%               286.7 kB / 8.289 GB   0.00%               9.48 kB / 648 B       98.3 kB / 0 B
$ 

si je trie sur le temps cpu, j'ai

3276efb16bf7        0.00%               241.7 kB / 8.289 GB   0.00%               8.832 kB / 648 B      0 B / 0 B
41769443a96d        0.07%               1.753 MB / 8.289 GB   0.02%               29.22 kB / 648 B      2.212 MB / 0 B
60e68fef3354        0.22%               271.3 MB / 8.289 GB   3.27%               0 B / 0 B             254.7 MB / 39.58 MB
72ebb165029b        0.01%               13.66 MB / 8.289 GB   0.16%               9.036 MB / 7.427 MB   458.8 kB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               36.38 kB / 648 B      10.34 MB / 0 B
e0e2560f1992        0.00%               286.7 kB / 8.289 GB   0.00%               9.48 kB / 648 B       98.3 kB / 0 B

ce qui n'est pas trié

En fait, cela trie une fois, je pensais à un script comme celui de pingouinux
https://forum.ubuntu-fr.org/viewtopic.php?id=1928881

qui trie constamment, je suppose qu'il faut voir si on a la ligne avec CONTAINER, puis mémoriser les lignes, et les afficher triées

Edit: le tri sur le 3 ème champ fonctionne bien

41769443a96d        0.08%               1.753 MB / 8.289 GB   0.02%               30.48 kB / 648 B      2.212 MB / 0 B
9a87e29ca5cf        0.01%               4.497 MB / 8.289 GB   0.05%               0 B / 0 B             7.447 MB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               37.64 kB / 648 B      10.34 MB / 0 B
72ebb165029b        0.00%               13.66 MB / 8.289 GB   0.16%               9.038 MB / 7.427 MB   458.8 kB / 0 B
3276efb16bf7        0.00%               241.7 kB / 8.289 GB   0.00%               10.09 kB / 648 B      0 B / 0 B
60e68fef3354        0.08%               271.4 MB / 8.289 GB   3.27%               0 B / 0 B             254.7 MB / 39.58 MB
e0e2560f1992        0.00%               286.7 kB / 8.289 GB   0.00%               10.74 kB / 648 B      98.3 kB / 0 B
92e96cd1c002        2.41%               311.3 kB / 8.289 GB   0.00%               0 B / 0 B             0 B / 0 B

et pas sur le 7 ème champ, bizarre

3276efb16bf7        0.00%               241.7 kB / 8.289 GB   0.00%               10.09 kB / 648 B      0 B / 0 B
41769443a96d        0.06%               1.753 MB / 8.289 GB   0.02%               30.48 kB / 648 B      2.212 MB / 0 B
60e68fef3354        0.21%               271.3 MB / 8.289 GB   3.27%               0 B / 0 B             254.7 MB / 39.58 MB
72ebb165029b        0.00%               13.66 MB / 8.289 GB   0.16%               9.038 MB / 7.427 MB   458.8 kB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               37.64 kB / 648 B      10.34 MB / 0 B
92e96cd1c002        4.76%               311.3 kB / 8.289 GB   0.00%               0 B / 0 B             0 B / 0 B
9a87e29ca5cf        0.00%               4.497 MB / 8.289 GB   0.05%               0 B / 0 B             7.447 MB / 0 B
e0e2560f1992        0.00%               286.7 kB / 8.289 GB   0.00%               10.74 kB / 648 B      98.3 kB / 0 B

en fait le -n de sort trie sur du numérique, donc les champs avec %, ça n'ira pas

Dernière modification par k3c (Le 27/01/2016, à 11:24)


Archlinux sur Xiaomi Air 13

Hors ligne

#4 Le 27/01/2016, à 11:31

k3c

Re : trier un flux en bash

docker stats | tee >(head -1) > >(tail -n +2 | sort -k 2.2); sleep 2

trie bien sur le temps CPU, merci credenhill

72ebb165029b        0.00%               13.66 MB / 8.289 GB   0.16%               9.038 MB / 7.427 MB   458.8 kB / 0 B
3276efb16bf7        0.00%               241.7 kB / 8.289 GB   0.00%               10.63 kB / 648 B      0 B / 0 B
e0e2560f1992        0.00%               286.7 kB / 8.289 GB   0.00%               11.28 kB / 648 B      98.3 kB / 0 B
9a87e29ca5cf        0.00%               4.497 MB / 8.289 GB   0.05%               0 B / 0 B             7.447 MB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               38.18 kB / 648 B      10.34 MB / 0 B
41769443a96d        0.07%               1.753 MB / 8.289 GB   0.02%               31.02 kB / 648 B      2.212 MB / 0 B
60e68fef3354        0.20%               270.3 MB / 8.289 GB   3.26%               0 B / 0 B             254.7 MB / 39.58 MB
92e96cd1c002        6.26%               430.1 kB / 8.289 GB   0.01%               0 B / 0 B             0 B / 0 B

Dernière modification par k3c (Le 27/01/2016, à 11:32)


Archlinux sur Xiaomi Air 13

Hors ligne

#5 Le 27/01/2016, à 11:43

k3c

Re : trier un flux en bash

La consommation mémoire triée par KB et MB

docker stats | tee >(head -1) > >(tail -n +2 | sort -k 4 -k 3.3 ); sleep 2

3276efb16bf7        0.00%               241.7 kB / 8.289 GB   0.00%               11.48 kB / 648 B      0 B / 0 B
e0e2560f1992        0.00%               286.7 kB / 8.289 GB   0.00%               12.12 kB / 648 B      98.3 kB / 0 B
92e96cd1c002        4.71%               495.6 kB / 8.289 GB   0.01%               0 B / 0 B             0 B / 0 B
41769443a96d        0.03%               1.753 MB / 8.289 GB   0.02%               31.86 kB / 648 B      2.212 MB / 0 B
9a87e29ca5cf        0.00%               4.497 MB / 8.289 GB   0.05%               0 B / 0 B             7.447 MB / 0 B
7779174e2a53        0.00%               8.065 MB / 8.289 GB   0.10%               39.02 kB / 648 B      10.34 MB / 0 B
72ebb165029b        0.00%               13.66 MB / 8.289 GB   0.16%               9.039 MB / 7.427 MB   458.8 kB / 0 B
60e68fef3354        0.10%               328.7 MB / 8.289 GB   3.97%               0 B / 0 B             255.3 MB / 70.06 MB

reste à trouver comment afficher constamment et pas juste une fois.


Archlinux sur Xiaomi Air 13

Hors ligne