#!/bin/bash

# Set file names
mylogname=$(date -u +$(hostname)_%Y%m%d_%H%M%S.tar)

# Create tar file with as first a text file that shows output of ls -lrt command
ls -lrt /var/log/edu_proxy/* >& /var/log/edu_proxy/ls-lrt.txt
tar -cf /tmp/$mylogname /var/log/edu_proxy/ls-lrt.txt

# Add 14 most recent edu_proxy logs
tar -rf /tmp/$mylogname $(ls -t /var/log/edu_proxy/edu_proxy_*.log* | head -14)

# Add 14 most recent service logs
tar -rf /tmp/$mylogname $(ls -t /var/log/edu_proxy/service_edu_proxy_*.log* | head -14)

# Add most upgrade logs
tar -rf /tmp/$mylogname $(ls -t /var/log/edu_proxy/deviceUpgrade*.log | head -3)

# Add list of deleted crashdumps
tar -rf /tmp/$mylogname /var/log/crash-dumps/deleted.log

# Add the output of the journalctl command
sudo journalctl -b -k -p err >& /tmp/journalctl.log
tar -rf /tmp/$mylogname /tmp/journalctl.log

# Add the output of the dmesg command
sudo dmesg -T >& /tmp/dmesg.log
tar -rf /tmp/$mylogname /tmp/dmesg.log

# Add most recent 2 syslog logs
sudo tar -rf /tmp/$mylogname $(ls -t /var/log/syslog* | head -2)

# Create tar file with as first a text file that shows output of ls -lrt command
ls -lrt /var/log/cinergy/* >& /var/log/cinergy/ls-lrt.txt
tar -rf /tmp/$mylogname /var/log/cinergy/ls-lrt.txt

# Add 8 most recent cinergy logs
tar -rf /tmp/$mylogname $(ls -t /var/log/cinergy/IC*.log | head -8)

# Add current cinergy config files
tar -rf /tmp/$mylogname /etc/cinergy/config/*

# Add current tmx config files
tar -rf /tmp/$mylogname /etc/barco-nrc-td-agent-bit/config/*

# Add current edu_proxy config files
tar -rf /tmp/$mylogname /etc/edu_proxy/config/*

# Collect process list and basic performance stats, collect into one text file
echo -e "System name:\n============\n" >> /tmp/systemInfo.txt
uname -a >> /tmp/systemInfo.txt
echo -e "\n\nDate and time when logfiles were captured:\n==========================================\n" >> /tmp/systemInfo.txt
date >> /tmp/systemInfo.txt
echo -e "\n\nDate and time inside edu-proxy logfiles:\n========================================\n" >> /tmp/systemInfo.txt
date -u >> /tmp/systemInfo.txt
echo -e "\n\nUptime\n=============================\n" >> /tmp/systemInfo.txt
uptime >> /tmp/systemInfo.txt
echo -e "\n\nDisk usage\n=============================\n" >> /tmp/systemInfo.txt
df -BG >> /tmp/systemInfo.txt
echo -e "\n\nNetwork details\n=============================\n" >> /tmp/systemInfo.txt
ip a >> /tmp/systemInfo.txt
echo -e "\n\nTop system and process output:\n=============================\n" >> /tmp/systemInfo.txt
top -b -n1 >> /tmp/systemInfo.txt
echo -e "\n\nNetwork port information\n=============================\n" >> /tmp/systemInfo.txt
netstat -a >> /tmp/systemInfo.txt

# Add systemInfo to tar
tar -rf /tmp/$mylogname /tmp/systemInfo.txt

# Zip to minimize file size, good for itrack attachments
gzip -9 /tmp/$mylogname

# Clean temporary files
rm -f /tmp/systemInfo.txt /tmp/dmesg.log /tmp/journalctl.log /var/log/edu_proxy/ls-lrt.txt /var/log/cinergy/ls-lrt.txt

# Let's make a clean exit...
exit 0
