#!/bin/bash
#Created by Foxzzz
#Vesion 2023.01
#Package Requirements

if [ `whoami` != root ]; then
  echo "Must be logged on as root to run this script."
  exit
fi

seinit() {
  CHECK=$(grep SELINUX= /etc/selinux/config | grep -v "#")
  if [ "$CHECK" == "SELINUX=enforcing" ]; then
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
  fi
  if [ "$CHECK" == "SELINUX=permissive" ]; then
    sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
  fi
 
  if [ "$CHECK" == "SELINUX=disabled" ]; then
    exit
  fi
}

sysinit() {
	grep "#Sihuainfo Support" /etc/sysctl.conf > /dev/null
	if [ $? -eq 0 ]; then
	    echo "SYSINIT OK"
	else
	    echo "    " >> /etc/sysctl.conf
	    echo "#Sihuainfo Support" >> /etc/sysctl.conf
            echo "vm.max_map_count=655360" >> /etc/sysctl.conf
            echo "fs.file-max=655360" >> /etc/sysctl.conf
            echo "vm.swappiness=1" >> /etc/sysctl.conf
            echo "net.core.somaxconn=4096" >> /etc/sysctl.conf
            echo "net.ipv4.tcp_max_tw_buckets=5000" >> /etc/sysctl.conf
            echo "net.ipv4.tcp_max_syn_backlog=1024" >> /etc/sysctl.conf
            echo "net.ipv4.ip_local_port_range=9000 65000" >> /etc/sysctl.conf
	    sysctl -p
	fi
}

hgpageinit() {
	grep "transparent_hugepage/enabled" /etc/rc.local > /dev/null
	if [ $? -eq 0 ]; then
	    echo "HUGEPAGEINIT OK"
	else
	    echo "touch /var/lock/subsys/local" >> /etc/rc.local
	    echo "if test -f /sys/kernel/mm/transparent_hugepage/enabled; then" >> /etc/rc.local
	    echo "  echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
	    echo "fi" >> /etc/rc.local
	fi
}

limitinit() {
	grep "#Sihuainfo Support" /etc/security/limits.conf > /dev/null
	if [ $? -eq 0 ]; then
	    echo "LIMITINIT OK"
	else
	    echo "    " >> /etc/security/limits.conf
	    echo "#Sihuainfo Support" >> /etc/security/limits.conf
	    echo "* soft nproc 2047" >> /etc/security/limits.conf
	    echo "* hard nproc 4096" >> /etc/security/limits.conf
	    echo "* soft nofile 65535" >> /etc/security/limits.conf
	    echo "* hard nofile 65535" >> /etc/security/limits.conf
	fi
}

osinfo() {
echo -e "-------------------------------System Information----------------------------"
echo -e "Hostname:\t\t"`hostname`
echo -e "uptime:\t\t\t"`uptime | awk '{print $3,$4}' | sed 's/,//'`
echo -e "Manufacturer:\t\t"`cat /sys/class/dmi/id/chassis_vendor`
echo -e "Product Name:\t\t"`cat /sys/class/dmi/id/product_name`
echo -e "Version:\t\t"`cat /sys/class/dmi/id/product_version`
echo -e "Serial Number:\t\t"`cat /sys/class/dmi/id/product_serial`
echo -e "Machine Type:\t\t"`vserver=$(lscpu | grep Hypervisor | wc -l); if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi`
echo -e "Operating System:\t"`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`
echo -e "Kernel:\t\t\t"`uname -r`
echo -e "Architecture:\t\t"`arch`
echo -e "Processor Name:\t\t"`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`
echo -e "Active User:\t\t"`w | cut -d ' ' -f1 | grep -v USER | xargs -n1`
echo -e "System Main IP:\t\t"`hostname -I`
echo ""
echo -e "-------------------------------CPU/Memory Usage------------------------------"
echo -e "Memory Usage:\t"`free | awk '/Mem/{printf("%.2f%"), $3/$2*100}'`
echo -e "Swap Usage:\t"`free | awk '/Swap/{printf("%.2f%"), $3/$2*100}'`
echo -e "CPU Usage:\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' |  awk '{print $0}' | head -1`
echo ""
echo -e "-------------------------------Disk Usage >80%-------------------------------"
df -Ph | sed s/%//g | awk '{ if($5 > 80) print $0;}'
echo ""
}

name='osinit.sh'  
case $1 in  
    s|init) echo "start init..."
        sysinit
	hgpageinit
    	limitinit
        seinit
    ;;
    check) echo "all ..."
        osinfo
    ;;
    *)echo "Usage: sh $name [init|check]"
    exit 1
    ;;
esac
