Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
apache:lecture_logs [2020/05/23 20:00] marclebrun |
apache:lecture_logs [2020/05/26 06:05] (Version actuelle) marclebrun [PHP] |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Lecture des Logs ====== | ====== Lecture des Logs ====== | ||
+ | |||
+ | ====== PHP ====== | ||
Exemple d'expression régulière : [[https://www.regexpal.com/95830]] | Exemple d'expression régulière : [[https://www.regexpal.com/95830]] | ||
+ | |||
+ | Description du format : [[https://httpd.apache.org/docs/2.4/fr/logs.html#accesslog]] | ||
+ | |||
+ | Tester sur [[https://regex101.com/]] | ||
<code reg> | <code reg> | ||
- | /^(\S+) (\S+) (\S+) \[([\w:/]+\s[+\-]\d{4})\] "(\S+)\s?(\S+)?\s?(\S+)?" (\d{3}|-) (\d+|-)\s?"?([^"]*)"?\s?"?([^"]*)?"?$/m | + | /^(\S+) (\S+) (\S+) \[([\w:\/]+\s[+\-]\d{4})\] "(\S+)\s?(\S+)?\s?(\S+)?" (\d{3}|-) (\d+|-)\s?"?([^"]*)"?\s?"?([^"]*)?"?$/m |
</code> | </code> | ||
- | ===== Logiciel(s) à tester ===== | + | Exemple : |
- | * GoAccess: [[https://goaccess.io/]] | + | <code php> |
+ | $re = '/^(\S+) (\S+) (\S+) \[([\w:\/]+\s[+\-]\d{4})\] "(\S+)\s?(\S+)?\s?(\S+)?" (\d{3}|-) (\d+|-)\s?"?([^"]*)"?\s?"?([^"]*)?"?$/m'; | ||
+ | $str = '91.178.82.190 - - [24/May/2020:11:07:25 +0200] "GET / HTTP/1.1" 302 4049 "https://www.marclebrun.be/pages" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0"'; | ||
+ | preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0); | ||
+ | |||
+ | print_r($matches); | ||
+ | |||
+ | $host = $matches[ 1][0]; // 91.178.82.190 | ||
+ | $datetime = $matches[ 4][0]; // 24/May/2020:11:07:25 +0200 | ||
+ | $method = $matches[ 5][0]; // GET | ||
+ | $request = $matches[ 6][0]; // / | ||
+ | $protocol = $matches[ 7][0]; // HTTP/1.1 | ||
+ | $status = $matches[ 8][0]; // 302 | ||
+ | $size = $matches[ 9][0]; // 4049 | ||
+ | $referer = $matches[10][0]; // https://www.marclebrun.be/pages | ||
+ | $userAgent = $matches[11][0]; // Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0 | ||
+ | </code> | ||
+ | |||
+ | ===== GoAccess ===== | ||
+ | |||
+ | Site web: [[https://goaccess.io/]] | ||
+ | |||
+ | ==== Installation ==== | ||
+ | |||
+ | Disponible dans les dépôts Ubuntu : | ||
+ | |||
+ | <code bash> | ||
+ | sudo apt install goaccess | ||
+ | </code> | ||
+ | |||
+ | ==== Lecture en ligne de commandes ==== | ||
+ | |||
+ | Se connecter au serveur par SSH. | ||
+ | |||
+ | <code bash> | ||
+ | sudo goaccess access.log | ||
+ | </code> | ||
+ | |||
+ | Appuyer sur **Escpace** afin de sélectionner **NCSA Combined Log format**. | ||
+ | Ensuite appuyer sur **Enter** pour accéder à l'interface en mode texte. | ||
+ | |||
+ | ^ Touche ^ Action ^ | ||
+ | | TAB / Shift+TAB | Naviguer entre les différents panels | | ||
+ | | ENTER | Déployer un panel pour afficher plus d'infos | | ||
+ | | J et K | Scroller (up/down) à l'intérieur d'un panel | | ||
+ | | Q | Quitter le panel déployé, ou quitter l'interface | | ||
+ | |||
+ | ==== Générer un fichier HTML ==== | ||
+ | |||
+ | <code bash> | ||
+ | sudo goaccess access.log -o report.html --log-format=COMBINED | ||
+ | </code> | ||