Informatique

Disk

scan to search for new device

1
# echo  1> /sys/class/block/sdb/device/rescan
# e2fsck -fy /dev/sdb
# resize2fs /dev/sdb
1
old
#  rescan-scsi-bus

Installé via la paquet : sg3_util ou scsitools

clamav

Whitelist

./eicar.com: Win.Test.EICAR_HSB-1 FOUND

1
echo "Win.Test.EICAR_HSB-1" >> /var/lib/clamav/whitelist.ign2

Un script de scan :

1
#!/usr/bin/env bash

# Scan and report for clamav
# by Jerem
# 20170313

TODAY=$(date +%Y.%m.%d)
LOGFILE=/var/log/clamav/clamav_scan.$TODAY.log
HOSTNAME=$(hostname)
#       --exclude=REGEX, --exclude-dir=REGEX

if [ -f $LOGFILE ]
        then rm -f $LOGFILE
fi

date > $LOGFILE
 
/usr/bin/clamscan -rio /data -l $LOGFILE

date >> $LOGFILE

if ((`/bin/grep Infected $LOGFILE | cut -d : -f2` > 0 ))
        then cat $LOGFILE | mail -s "Antivirus scan on $HOSTNAME" -a "From:supervision@plop.com" jerem@mail.fr
fi

Bench

Bench CPU via dd :

Pour 4 proc

1
fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd

Alerta

DOC : http://docs.alerta.io/en/latest/

1
apt-get install postfix
apt-get install mongodb
apt-get install python-pip
pip install --upgrade pip
pip install alerta-server
1
export ALERTA_SVR_CONF_FILE=/etc/alertad.conf
mkdir /var/log/alerta/
1
cat /etc/alertad.conf

# Conf Alerta
# http://docs.alerta.io/en/latest/configuration.html

# BASIC
DEBUG = False
BASE_URL = ''
LOGGER_NAME = 'alerta'
LOG_FILE = '/var/log/alerta/alertad.log'

# MONGODB
MONGO_URI = 'mongodb://localhost:27017/monitoring'
MONGO_DATABASE = 'monitoring'

# CORS
CORS_ORIGINS = [
    'http://127.0.0.1',
    'http://localhost',
    'http://192.168.50.45:8000',
    'http://192.168.50.45',
    'http://192.168.50.40'
]

# AUTH
AUTH_REQUIRED = False
SECRET_KEY = ''

ADMIN_USERS = ['astreinte@mail.fr']
CUSTOMER_VIEWS = False

#OAUTH2_CLIENT_ID = None  # Google or GitHub OAuth2 client ID and secret
#OAUTH2_CLIENT_SECRET = None
#ALLOWED_EMAIL_DOMAINS = ['*']
#ALLOWED_GITHUB_ORGS = ['*']
#
#GITLAB_URL = None
#ALLOWED_GITLAB_GROUPS = ['*']

TOKEN_EXPIRE_DAYS = 7

# API
QUERY_LIMIT = 10000
HISTORY_LIMIT = 100
API_KEY_EXPIRE_DAYS = 365

AUTO_REFRESH_ALLOW = 'ON'
SENDER_API_ALLOW = 'ON'

# EMAIL
EMAIL_VERIFICATION = False
SMTP_HOST = 'localhost'
SMTP_PORT = 25
MAIL_FROM = 'alerta@plop.com'
SMTP_PASSWORD = ''

# SUP
BLACKOUT_DURATION = 86400

SEVERITY_MAP = {
    'security': 0,
    'critical': 1,
    'major': 2,
    'minor': 3,
    'warning': 4,
    'indeterminate': 5,
    'cleared': 5,
    'normal': 5,
    'ok': 5,
    'informational': 6,
    'debug': 7,
    'trace': 8,
    'unknown': 9
}
DEFAULT_SEVERITY = 'indeterminate'

# Plug-ins
PLUGINS = ['reject','amqp','alerta-mailer']

# amqp https://github.com/alerta/alerta-contrib/tree/master/plugins/amqp
AMQP_URL = 'mongodb://localhost:27017/kombu'
AMQP_TOPIC = 'notify'

ORIGIN_BLACKLIST = ['foo/bar$', '.*/qux']  # reject all foo alerts from bar, and everything from qux
ALLOWED_ENVIRONMENTS = ['blabla']  # reject alerts without allowed environments
1
cat alertad.sh 
#!/bin/bash


### BEGIN INIT INFO
# Provides: alertad
# Required-Start: $remote_fs $all
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Description: Starts and stops alertad
### END INIT INFO


ABIN=/usr/local/bin/alertad


case $1 in

# Startup alertad
        start)

                $ABIN &
                echo "starting..."
        ;;

# Stop alertad
        stop)
                killall alertad
                echo "stopping..."
        ;;

        *)
                echo "stop ou start c'est tout"
        ;;
esac

alerta webui

DOC : http://docs.alerta.io/en/latest/webui.html

1
cd /opt
git clone https://github.com/alerta/angular-alerta-webui
cd /opt/angular-alerta-webui/app/
1
cat config.js

'use strict';

angular.module('config', [])
  .constant('config', {
    'endpoint'    : "http://"+window.location.hostname+":8080",
    'provider'    : "basic", // google, github, gitlab or basic
//    'client_id'   : "",
//    'gitlab_url'  : "https://gitlab.com",  // replace with your gitlab server
    'colors'      : {}, // use default colors
//    'colors'      : {
//      'severity': {
//        'critical'     : '#D8122A',
//        'major'        : '#EA680F',
//        'minor'        : '#FFBE1E',
//        'warning'      : '#BA2222',
//        'indeterminate': '#A6ACA8',
//        'cleared'      : '#00AA5A',
//        'normal'       : '#00AA5A',
//        'ok'           : '#00AA5A',
//        'informational': '#00A1BC',
//        'debug'        : '#9D006D',
//        'security'     : '#333333',
//        'unknown'      : '#A6ACA8'
//      },
//      'text': 'white',
//      'highlight': 'lightgray'
//    },
    'severity'    : {}, // use default severity codes
//    'severity'    : {
//      'fatal': 0
//    },
    'audio'       : {}, // no audio
//    'audio'       : {
//      'new'  : '/audio/Bike Horn.mp3'
//    }
//    'tracking_id' : ""  // Google Analytics tracking ID eg. UA-NNNNNN-N
  });
1
cat /etc/init.d/alerta-webui.sh
#!/bin/bash


### BEGIN INIT INFO
# Provides: alerta-webui
# Required-Start: $remote_fs $all
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Description: Starts and stops alerta-webui
### END INIT INFO


AWDIR=/opt/angular-alerta-webui/app/


case $1 in

# Startup alerta-webui
        start)

                cd $AWDIR && python -m SimpleHTTPServer 8000 &
                echo "starting..."
        ;;

# Stop alerta-webui
        stop)
                for i in  ` ps auxw | grep SimpleHTTPServer | grep -v grep | /usr/bin/awk '{print $2}'` ; do kill -9 $i ; done
                echo "stopping..."
        ;;

        *)
                echo "stop ou start c'est tout"
        ;;
esac

alerta mailer (& alerta-amqp)

Doc : https://github.com/alerta/alerta-contrib/tree/master/integrations/mailer

1
git clone https://github.com/alerta/alerta-contrib.git
cd /opt/alerta-contrib/plugins/amqp
python setup.py install
1
cd /opt/alerta-contrib/integrations/mailer
vi mailer.py
-    'config_file':   '~/.alerta.conf',
+    'config_file':   '/etc/alerta-mailer.conf',
1
python setup.py install
1
cat /etc/alerta-mailer.conf

[alerta-mailer]
key = 
mail_to = astreinte@mail.fr
mail_from = alerta@plop.com
dashboard_url = http://192.168.50.45:8000
endpoint =  http://192.168.50.45:8080
smtp_host = 127.0.0.1
smtp_port = 25
smtp_password = 
smtp_use_ssl = False
debug = True
skip_mta = False
email_type = html
mail_localhost = plop.com
amqp_url = mongodb://127.0.0.1:27017/kombu
amqp_topic = notify
mail_template = /opt/alerta-contrib/integrations/mailer/email.tmpl
mail_template_html = /opt/alerta-contrib/integrations/mailer/email.html.tmpl

alerta-mailer

1
cat /etc/cron.d/alerta-mailer_restart 
0 */2 * * * root /etc/init.d/alerta-mailer-init.sh stop && /bin/sleep 2 && /etc/init.d/alerta-mailer-init.sh start >> /var/log/alerta/restart 2>&1
1
cat /etc/init.d/alerta-mailer-init.sh

#!/bin/bash


### BEGIN INIT INFO
# Provides: alerta-mailer
# Required-Start: $remote_fs $all
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Description: Starts and stops alerta-mailer.
### END INIT INFO


AMBIN=/usr/local/bin/alerta-mailer


case $1 in

# Startup alerta-mailer
        start)

                $AMBIN &
                echo "starting..."
        ;;

# Stop alerta-mailer
        stop)
                killall alerta-mailer
                echo "stopping..."
        ;;

        *)
                echo "stop ou start c'est tout"
        ;;
esac

Docker

DOCKER

  • INTRODUCTION

registry publique

https://hub.docker.com/

Orchestration

compose(single)/swarm(multi host)
http://mesos.apache.org/

IMAGES manipulation

registry

1
~# docker search ubuntu
~# docker pull ubuntu:latest
~# docker push jeba/ubuntu

tag (rename)

1
~# docker tag jeba/ubuntu jeba/plop

details

1
~# docker inspect jeba/ubuntu | jq

run

daemon

1
~# docker run -d

interactiv (stdin) -t terminal

1
~# docker run -it --name myubuntu ubuntu bash

–rm : rm on stop
–restart always

exit whithout kill

ctrl+p+q

re attach

1
~# docker container attach [ID]

execute

1
~# docker container exec -i -t [id] <command>

logs

1
~# docker container logs
~# docker container logs -f
~# docker container logs --tail 20

stop/kill

1
~# docker container stop    (sigterm)
~# docker container kill    (sigkill)

bind

1
~# docker container run -d -p 80:80 nginx
~# docker container exec -it nginx bash

commit (save)

1
~# docker container commit nginx mon_nginx:0.1

cleaning

1
~# docker container prune
~# docker image prune

dockerfile

https://docs.docker.com/engine/reference/builder/

FROM : image de base
RUN : modif du fs
COPY : cp from host to container
ADD : cp also from an URL
ENTRYPOINT : run at start of the container
CMD : run at start of the container (can be overridden)( parameters for ENTRYPOINT (if exist))
ENV : set env variables
ARG : set variables
USER : set user
WORKDIR : set pwd
VOLUME
EXPOSE
SHELL
LABEL
HEALTHCHECK
STOPSIGNAL

Build image from dockerfile

1
~# docker image build -t name:tag <context_path> ( -f <file>  if not "Dockerfile" )
1
~# cat Dockerfile
FROM ubuntu:latest
RUN apt-get update  && \
    apt-get install iputils-ping -y
VOLUME /var/www
ENTRYPOINT ["ping"]
CMD ["8.8.8.8"]

VOLUMES

1
~# docker container run -v /home/wam:/container/home/
~# docker container run -v /home/wam:/container/home/:ro

~# docker volume create voltest  (--driver local --opt type=btrfs --opt device=/dev/sda2)  (OR --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=100m,uid=1000)
~# ~# ls /var/lib/docker/volumes/voltest/
~# docker container run -v voltest:/container/home/

NETWORK

map to host (host:container)

1
~# docker container run -d -p 80:80 nginx

auto map (on random port)

1
~# docker container run -d -P nginx

EXPOSE in dockerfile is informative !

1
~# docker network create --driver bridge mynetwork
~# docker network create --driver bridge --subnet 10.0.0.1/24 mynetwork

no dns resolv on docker0 (auto create bridge)
dns resolv auto activate on manually created bridge

ex:

1
~# docker network create -d bridge --subnet 192.168.69.0/24 testnet
~# docker run -d -P --network=testnet jeba/nginx:0.1

ENV VAR

export DOCKER_TLS_VERIFY=”1”
export DOCKER_HOST=”tcp://127.0.0.1:2375”
export DOCKER_CERT_PATH=”/home/jerem/.docker/certs”
export DOCKER_MACHINE_NAME=”myDock”

TIPS

Info

1
~# docker system

Clean

1
~# docker system prune

Exit/detach

1
~# ctrl+p+q

rm all

1
~# docker rm -f $(docker ps -aq)

Expose dockerengine port and use insecure registry

1
~# cat /lib/systemd/system/docker.service
 ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
 ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 -insecure-registry myregistry:5000
 ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 --tlsverify --tlscacert=/etc/docker/certs.d/ca.pem --tlscert=/etc/docker/certs.d/server-cert.pem --tlskey=/etc/docker/certs.d/server-key.pem

Create stack = overlay network and services (on swarm with compose)

1
~# docker stack deploy -c compose.yml mystack

Traefik

load balancer
https://docs.traefik.io/

Portainer

Webgui for swarm (or not)

http://portainer.io/

Kubernetes

https://kubeapps.com/

https://github.com/kubernetes/helm
http://helm.sh/

https://github.com/kubernetes/minikube

Rancher

http://rancher.com/

Others

https://www.nomadproject.io/
https://www.terraform.io/docs/providers/docker/index.html

List 10 last versions of an image on registry

curl ‘https://registry.hub.docker.com/v2/repositories/heketi/heketi/tags/'|jq ‘.”results”[][“name”]’

Raid

Copy table

1
sfdisk -d /dev/sdb | sfdisk -f /dev/sda

Create raid

1
mdadm --create --verbose /dev/md2 --level=1 --raid-devices=2 /dev/sdc /dev/sdd

Add disk to raid

1
mdadm --manage /dev/md0 --add /dev/sda1

Speed up rebuild

1
echo 50000 > /proc/sys/dev/raid/speed_limit_min

SSH

Escape sequences

1
~# ~?
Supported escape sequences:
 ~.   - terminate connection (and any multiplexed sessions)
 ~B   - send a BREAK to the remote system
 ~C   - open a command line
 ~R   - request rekey
 ~V/v - decrease/increase verbosity (LogLevel)
 ~^Z  - suspend ssh
 ~#   - list forwarded connections
 ~&   - background ssh (when waiting for connections to terminate)
 ~?   - this message
 ~~   - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

restrict ssh acces

1
~# grep AllowUsers /etc/ssh/sshd_config
AllowUsers root@93.14.29.25 jerem none

use keepalive

1
ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=1 10.0.0.1

ssh tunnel

  • local tunnel

-L : local
-f : background
-n : redirect stdin > /dev/null

1
~# ssh -L 8080:localhost:80 jerem.unvrai.info -f -n
~# curl http://localhost:8080
<h1> unvrai.info welcome page </h1>
1
~# ssh -L 8080:web.unvrai.info:80 jerem.unvrai.info -f -n
~# curl http://localhost:8080
<h1> web.unvrai.info home page </h1>
  • remote tunnel
1
~# ssh -R 8080:localhost:80 jerem@unvrai.info -f -n
~# curl http://unvrai.info:8080
<h1> my local welcome page </h1>
  • SSH connection through host in the middle

    1
    ~# ssh -t reachable_host ssh unreachable_host
  • Testing remote connection to port

    1
    timeout 1 bash -c "</dev/<proto>/<host>/<port>" >/dev/null 2>&1 ; echo $?

sshfs

1
~$ mkdir /opt/doc/ansible
~$ sshfs jerem@server.fr:doc/ansible /opt/doc/ansible
1
~# grep sshfs /etc/fstab
user@machine:/répertoire/distant                /mnt/mon_rep          fuse.sshfs           port=22,user,noatime,_netdev     0 0

Forward X

1
~# grep X11 /etc/ssh/sshd_config
X11Forwarding yes
#X11DisplayOffset 10
X11UseLocalhost no

Clean keys

1
~# ssh-keygen -f "/home/jerem/.ssh/known_hosts" -R 10.0.0.10

logrotate

1
/var/log/apache2/*.log {
        daily
        missingok
        rotate 366
        compress
        compresscmd /bin/bzip2
        compressext .bz2
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                /etc/init.d/apache2 reload > /dev/null
        endscript
}

Elasticsearch

Curator est un outil qui va permettre de purger d’anciens indices ou snapshot ES

Need :

  • Elasticsearch
  • python-pip

Install

1
~# pip install elasticsearch-curator==2.1.2

Usage

1
$> curator delete --prefix .marvel- --older-than 15

Exemple de cron

1
15 3 * * * elasticsearch /usr/local/bin/curator --host 127.0.0.1 delete --prefix logstash- --older-than 360 --time-unit days --timestring '%Y.%m.%d'

check du cluster

1
~# curl -XGET 'http://localhost:9200/_cluster/health?pretty'

node info

1
~# curl -XGET 'localhost:9200/_nodes?pretty'

Some stats

1
~# curl -XGET 'localhost:9200/_stats?pretty'

Some docs

Cluster guide
prod checklist