Skip to content

Instantly share code, notes, and snippets.

@ivanrosolen
Created November 2, 2018 02:22
Show Gist options
  • Save ivanrosolen/a9fe3de4be21210cf9da061256b63168 to your computer and use it in GitHub Desktop.
Save ivanrosolen/a9fe3de4be21210cf9da061256b63168 to your computer and use it in GitHub Desktop.
Machine Monitor Telegram Message
<?php
// Create bot
// https://core.telegram.org/bots#6-botfather
//
//
// Get Channel ID - https://api.telegram.org/bot$api_token/getUpdates
// chat": { "id": -123456,
// Telegram
$api_token = 'xxx';
$chat_id = 'xxx';
// Database
$host = '127.0.0.1';
$db = 'xxx';
$user = 'xxx';
$pass = 'xxx';
$charset = 'utf8mb4';
// Rabbit
$rabbit_api = 'http://localhost:15672/api/queues/%2f/QUEUE-NAME';
$rabbit_auth = 'user:pwd';
$dsn = 'mysql:host='.$host.'dbname='.$db.';charset='.$charset;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $options);
$users = $pdo->query("SELECT COUNT(name) as total FROM users;")->fetch();
$list_files = shell_exec('ls -1 /PATH/TO/FILES | wc -l');
$list_files = (int)$list_files;
$processes_running = shell_exec('ps x | grep process_name ');
$processes_running_total = substr_count($processes_running, '/bin/bash ./process_name');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$rabbit_api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $rabbit_auth);
$result=curl_exec ($ch);
curl_close ($ch);
$rabbit = json_decode($result,true);
$msg = "Report:
Users: ".$users['total']."
Files: ".$list_files."
RabbitMQ Queue Messages: ".$rabbit['messages']."
RabbitMQ Queue Consumers: ".$rabbit['consumers']."
Processes: ".$processes_running_total;
$data = array(
'chat_id' => $chat_id,
'text' => $msg
);
$url = 'https://api.telegram.org/bot'.$api_token.'/sendMessage?' . http_build_query($data);
$response = file_get_contents($url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment