Add atop

Change-Id: I59297736406469e5314236cb40851d9a6f94386e
diff --git a/README.rst b/README.rst
index 451cadf..620f582 100644
--- a/README.rst
+++ b/README.rst
@@ -687,6 +687,18 @@
 
 Possible status is dead (disable service by default), running (enable service by default), enabled, disabled.
 
+Linux with atop service:
+
+.. code-block:: yaml
+
+    linux:
+      system:
+        atop:
+          enabled: true
+          interval: 20
+          logpath: "/var/log/atop"
+          outfile: "/var/log/atop/daily.log"
+
 RHEL / CentOS
 ^^^^^^^^^^^^^
 
diff --git a/linux/files/atop.conf b/linux/files/atop.conf
new file mode 100644
index 0000000..4474da7
--- /dev/null
+++ b/linux/files/atop.conf
@@ -0,0 +1,5 @@
+{%- from "linux/map.jinja" import system with context -%}
+# This file /etc/default/atop is managed by Salt linux formula
+INTERVAL={{ system.atop.interval }}
+LOGPATH={{ system.atop.logpath }}
+OUTFILE={{ system.atop.outfile }}
diff --git a/linux/files/atop.systemd b/linux/files/atop.systemd
new file mode 100644
index 0000000..be57dbb
--- /dev/null
+++ b/linux/files/atop.systemd
@@ -0,0 +1,16 @@
+{%- from "linux/map.jinja" import system with context -%}
+[Unit]
+Description=atop - advanced interactive monitor
+After=syslog.target
+ConditionPathExists={{ config_file }}
+Documentation=man:atop(1)
+Documentation=https://atoptool.nl
+
+[Service]
+EnvironmentFile=-{{ config_file }}
+ExecStart=/usr/bin/atop -a -w ${LOGPATH}/daily.log ${INTERVAL}
+Restart=always
+RestartSec=10
+
+[Install]
+WantedBy=multi-user.target
diff --git a/linux/map.jinja b/linux/map.jinja
index e4f2b10..d6ffc4f 100644
--- a/linux/map.jinja
+++ b/linux/map.jinja
@@ -18,6 +18,13 @@
          },
         'selinux': 'permissive',
         'ca_certs_dir': '/usr/local/share/ca-certificates',
+        'atop': {
+             'enabled': false,
+             'interval': '20',
+             'autostart': true,
+             'logpath': '/var/log/atop',
+             'outfile': '/var/log/atop/daily.log'
+         },
     },
     'Debian': {
         'pkgs': ['python-apt', 'apt-transport-https', 'libmnl0'],
@@ -38,6 +45,13 @@
          },
         'selinux': 'permissive',
         'ca_certs_dir': '/usr/local/share/ca-certificates',
+        'atop': {
+             'enabled': false,
+             'interval': '20',
+             'autostart': true,
+             'logpath': '/var/log/atop',
+             'outfile': '/var/log/atop/daily.log'
+         },
     },
     'RedHat': {
         'pkgs': ['policycoreutils', 'policycoreutils-python', 'telnet', 'wget'],
@@ -58,6 +72,13 @@
          },
         'selinux': 'permissive',
         'ca_certs_dir': '/usr/local/share/ca-certificates',
+        'atop': {
+             'enabled': false,
+             'interval': '20',
+             'autostart': true,
+             'logpath': '/var/log/atop',
+             'outfile': '/var/log/atop/daily.log'
+         },
     },
 }, grain='os_family', merge=salt['pillar.get']('linux:system')) %}
 
diff --git a/linux/meta/logrotate.yml b/linux/meta/logrotate.yml
new file mode 100644
index 0000000..cd980f7
--- /dev/null
+++ b/linux/meta/logrotate.yml
@@ -0,0 +1,20 @@
+{%- from "linux/map.jinja" import system with context -%}
+
+{%- if system.atop.enabled %}
+job:
+  atop:
+    - files:
+        - {{ system.atop.logpath }}/atop*
+        - {{ system.atop.logpath }}/{{ system.atop.outfile }}
+      options:
+        - olddir {{ system.atop.logpath }}/old
+        - compress
+        - delaycompress
+        - missingok
+        - notifempty
+        - rotate: 10
+        - daily
+        - minsize: 20M
+        - maxsize: 500M
+        - postrotate: "if ! service atop status > /dev/null; then service atop restart > /dev/null; fi"
+{%- endif %}
diff --git a/linux/system/atop.sls b/linux/system/atop.sls
new file mode 100644
index 0000000..e31db8a
--- /dev/null
+++ b/linux/system/atop.sls
@@ -0,0 +1,76 @@
+{%- from "linux/map.jinja" import system with context %}
+
+{%- if system.atop.enabled %}
+
+atop_packages:
+  pkg.installed:
+    - name: atop
+
+atop_defaults:
+  file.managed:
+    - name: /etc/default/atop
+    - source: salt://linux/files/atop.conf
+    - template: jinja
+    - user: root
+    - group: root
+    - mode: 644
+
+atop_logpath:
+  file.directory:
+  - name: {{ system.atop.logpath }}
+  - user: root
+  - group: root
+  - mode: 750
+  - makedirs: true
+
+{%- if grains.get('init', None) == 'systemd' %}
+atop_systemd_file:
+  file.managed:
+  - name: /etc/systemd/system/atop.service
+  - source: salt://linux/files/atop.service
+  - user: root
+  - mode: 644
+  - defaults:
+    service_name: atop
+    config_file: /etc/default/atop
+    autostart: {{ system.atop.autostart }}
+  - template: jinja
+  - require_in:
+    - service: atop_service
+{%- endif %}
+
+atop_service:
+  service.running:
+    - name: atop
+    - enable: {{ system.atop.autostart }}
+    - watch:
+      - file: atop_defaults
+    {%- if grains.get('noservices') %}
+    - onlyif: /bin/false
+    {%- endif %}
+
+{%- else %}
+
+atop_service_stop:
+  service.dead:
+    - name: atop
+    - enable: false
+    - require_in:
+      - pkg: atop_pkg_purge
+    {%- if grains.get('noservices') %}
+    - onlyif: /bin/false
+    {%- endif %}
+
+atop_defaults_purge:
+  file.absent:
+    - names:
+      - /etc/default/atop
+      - /etc/systemd/system/atop.service
+    - require:
+      - pkg: atop_pkg_purge
+
+atop_pkg_purge:
+  pkg.purged:
+    - name: atop
+
+{%- endif %}
diff --git a/linux/system/init.sls b/linux/system/init.sls
index 0ba87aa..2f379f4 100644
--- a/linux/system/init.sls
+++ b/linux/system/init.sls
@@ -63,6 +63,9 @@
 {%- if system.apparmor is defined %}
 - linux.system.apparmor
 {%- endif %}
+{%- if pillar.linux.system.atop is defined %}
+- linux.system.atop
+{%- endif %}
 {%- if system.console is defined %}
 - linux.system.console
 {%- endif %}
diff --git a/tests/pillar/system.sls b/tests/pillar/system.sls
index f39fdde..411323c 100644
--- a/tests/pillar/system.sls
+++ b/tests/pillar/system.sls
@@ -346,3 +346,8 @@
         192.168.0.1:
           mac: "ff:ff:ff:ff:ff:ff"
           interface: bond0
+    atop:
+      enabled: true
+      interval: 20
+      logpath: "/var/mylog/atop"
+      outfile: "/var/mylog/atop/daily.log"