├── README.md └── demo.php /README.md: -------------------------------------------------------------------------------- 1 | # LinuxServerMonitorTelegramBot 2 | Monitor your Linux Server on Telegram with this bot 3 | 4 | With this bot for telegram you can monitor your Linux server. 5 | Tested in 6 | - CentOS 5x 7 | - CentOS 6x 8 | 9 | #Telegram demo 10 | @LinuxMonitorBot 11 | 12 | #Installation 13 | - Download server.php 14 | - Chat in Telegram with [@BotFather](https://t.me/BotFather) and create a new bot 15 | - Get your token key 16 | - Rename demo.php to another name 17 | - In php file, line 7, change (token) for your token 18 | - Upload to your server with SSL support 19 | - Set webhook in: https://api.telegram.org/bot(token)/setWebhook?url=https://(yourDomain/yourFile.php) 20 | - Add following lines to /etc/sudoers, where username is your account name's domain: 21 | ``` 22 | username ALL = NOPASSWD: /usr/sbin/csf 23 | username ALL = NOPASSWD: /scripts/cphulkdwhitelist 24 | username ALL = NOPASSWD: /bin/grep 25 | username ALL = NOPASSWD: /bin/ls 26 | username ALL = NOPASSWD: /bin/ping 27 | username ALL = NOPASSWD: /usr/bin/top 28 | username ALL = NOPASSWD: /usr/bin/whois 29 | ``` 30 | - Open conversation in Telegram with your bot searching @yourBotName 31 | - Enjoy 32 | 33 | #Commands 34 | **/apacheerrorlog** - show last Apache error log 35 | 36 | **/eximsendmaillog** - show exim sender's path and count 37 | 38 | **/firewall** - add IP to CSF and cpHulk whitelist (CSF, cPanel required) 39 | 40 | **/free** - show free memory in MB 41 | 42 | **/last** - Show last 20 system login, ip date 43 | 44 | **/ls** - list a directory content 45 | 46 | **/ping** - ping to IP or domain 47 | 48 | **/ps** - show process with filter 49 | 50 | **/top** - show system top process 51 | 52 | **/uptime** - show server uptima 53 | 54 | **/versions** - show OS, apache, PHP and MySQL versions 55 | 56 | **/w** - show who is connected to server 57 | 58 | **/whois** - show domains info 59 | -------------------------------------------------------------------------------- /demo.php: -------------------------------------------------------------------------------- 1 | '); 8 | define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/'); 9 | 10 | //OBTENER INFORMACION 11 | $content = file_get_contents("php://input"); 12 | $update = json_decode($content, true); 13 | $chatID = $update["message"]["chat"]["id"]; 14 | $text = $update['message']['text']; 15 | $file = "$chatID/$chatID.txt"; 16 | 17 | if ($text == "") 18 | die(); 19 | 20 | 21 | //CREAR DIRECTORIO PARA ESTADOS 22 | if (!file_exists($chatID)) : 23 | mkdir($chatID, 0700, true); 24 | fopen($file, 'w'); 25 | endif; 26 | 27 | 28 | //COMANDOS 29 | switch ($text): 30 | 31 | case "/apacheerrorlog": 32 | file_put_contents($file, ''); 33 | $msg = shell_exec("tail /var/log/httpd/error_log"); 34 | break; 35 | 36 | case "/eximsendmaillog": 37 | file_put_contents($file, ''); 38 | $msg = shell_exec("/usr/bin/sudo grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F\"cwd=\" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n"); 39 | break; 40 | 41 | case "/firewall": 42 | $status = "$text,0"; 43 | file_put_contents($file, $status); 44 | $msg = "Escribe la ip que se pondrá en lista blanca"; 45 | break; 46 | 47 | case "/free": 48 | file_put_contents($file, ''); 49 | $msg = shell_exec("free -m"); 50 | break; 51 | 52 | case "/last": 53 | file_put_contents($file, ''); 54 | $msg = shell_exec("last -20"); 55 | break; 56 | 57 | case "/ls": 58 | $status = "$text,0"; 59 | file_put_contents($file, $status); 60 | $msg = "Escribe la ruta a listar"; 61 | break; 62 | 63 | case "/ping": 64 | $status = "$text,0"; 65 | file_put_contents($file, $status); 66 | $msg = "Escribe el dominio o IP"; 67 | break; 68 | 69 | case "/ps": 70 | $status = "$text,0"; 71 | file_put_contents($file, $status); 72 | $msg = "Escribe el texto a filtrar en los procesos"; 73 | break; 74 | 75 | case "/start" : 76 | file_put_contents($file, ''); 77 | $msg = "Bienvenido a Linux Server Monitor"; 78 | break; 79 | 80 | case "/top" : 81 | file_put_contents($file, ''); 82 | $msg = shell_exec("/usr/bin/sudo top -b -n 1 | head -n 15"); 83 | break; 84 | 85 | case "/uptime": 86 | file_put_contents($file, ''); 87 | $msg = shell_exec("uptime"); 88 | break; 89 | 90 | case "/versions": 91 | file_put_contents($file, ''); 92 | $cen = shell_exec("cat /etc/redhat-release"); 93 | $des = shell_exec("ls -lct --time-style=+\"%F %T\" / | tail -1 | awk '{print $6, $7}'"); 94 | $apa = shell_exec("/usr/sbin/apachectl -v | grep 'Server version:'"); 95 | $php = phpversion(); 96 | $mys = shell_exec("mysql -V"); 97 | $msg = "S.O.: $cen $des"; 98 | $msg.= "Apache: $apa"; 99 | $msg.= "PHP: $php"; 100 | $msg.= "\nMySQL: $mys"; 101 | break; 102 | 103 | case "/w": 104 | file_put_contents($file, ''); 105 | $msg = shell_exec("w"); 106 | break; 107 | 108 | case "/whois": 109 | $status = "$text,0"; 110 | file_put_contents($file, $status); 111 | $msg = "Escribe el dominio a buscar"; 112 | break; 113 | 114 | default: 115 | //SI YA SE ESTÁ EJECUTANDO UN COMANDO 116 | $st = explode(",", file_get_contents($file)); 117 | 118 | switch ($st[0]): 119 | 120 | case "/firewall": 121 | $ip = $text; 122 | if (!filter_var($ip, FILTER_VALIDATE_IP) === false) : 123 | $csf = shell_exec("/usr/bin/sudo /usr/sbin/csf -a $ip Bot consoluciones"); 124 | $hulk = shell_exec("/usr/bin/sudo /scripts/cphulkdwhitelist $ip"); 125 | $msg = "CSF: $csf\n"; 126 | $msg.= "CPHulk: $hulk"; 127 | else : 128 | $msg = "$ip no es una IP válida"; 129 | endif; 130 | $msg.= "\nPuede escribir otra IP"; 131 | break; 132 | 133 | case "/ls": 134 | $msg = shell_exec("/usr/bin/sudo ls -lh $text"); 135 | $msg.= "\nPuede escribir otra ruta a listar"; 136 | break; 137 | 138 | case "/ping": 139 | $msg = shell_exec("/usr/bin/sudo ping $text -c 4"); 140 | $msg.= "\nPuede escribir otro dominio o IP"; 141 | break; 142 | 143 | case "/ps": 144 | $msg = shell_exec("ps aux | grep $text"); 145 | $msg.= "\nPuede escribir otra palabra para filtrar"; 146 | break; 147 | 148 | case "/whois": 149 | $msg = shell_exec("/usr/bin/sudo whois $text"); 150 | $msg.= "\nPuede escribir otro dominio a buscar"; 151 | break; 152 | endswitch; 153 | 154 | break; 155 | endswitch; 156 | 157 | $sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".urlencode($msg); 158 | file_get_contents($sendto); 159 | ?> 160 | --------------------------------------------------------------------------------