Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php5
- <?php
- // Wow, getopt() is a stupid function that just barely works :(
- $options = getopt('u:p:h:');
- // Lets see if the user has a bitcoin.conf file in the usual location of $HOME/.bitcoin/bitcoin.conf
- $bcconf = getenv('HOME') . '/.bitcoin/bitcoin.conf';
- if(file_exists($bcconf)) {
- // Looks like they have a config file, so lets see if we can find the username and password
- $bcconf_text = file_get_contents($bcconf);
- $bcconf_text = explode("\n",$bcconf_text);
- $username = $password = NULL;
- // Now we'll just scan through each line of the file looking for the username and password
- foreach($bcconf_text as $line) {
- $setting = substr($line,0,strpos($line,'='));
- $data = substr($line,strpos($line,'=') + 1);
- if($setting == "rpcuser" && $username == NULL) {
- // Found the username!
- $rpcuser = $data;
- } elseif($setting == "rpcpassword" && $password == NULL) {
- // Found the password!
- $rpcpass = $data;
- }
- }
- }
- // If -u or $rpcuser aren't set we'll assume a default username of 'user' since leaving this blank doesn't work with php's fopen()
- if(isset($options['u'])) {
- $rpcuser = $options['u'];
- } elseif(empty($rpcuser)) {
- $rpcuser = 'user';
- }
- // We need a password to be set
- if(isset($options['p'])) {
- $rpcpass = $options['p'];
- } elseif(empty($rpcpass)) {
- // The user didn't specify a password at the command line and didn't have one in his bitcoin.conf :(
- echo "Usage: {$argv[0]} -p <password> [-u username] [-h hostname/IP]\n";
- // Why not exit with 1? Because then nagios would think this was a warning
- die(255);
- }
- // If -h isn't set we'll assume that the hostname is 'localhost'
- if(!isset($options['h'])) {
- $rpchost = 'localhost';
- } else {
- $rpchost = $options['h'];
- }
- try {
- $bitcoin = new jsonRPCClient("http://{$rpcuser}:{$rpcpass}@{$rpchost}:8332/");
- $info = $bitcoin->getinfo();
- } catch (Exception $e) {
- // The node is down! I think I'm leaking!
- echo "CRITICAL: Bitcoin is down!\n";
- die(2);
- }
- // Convert from khps to hps. Why? Because this way my graphs don't have weird units of measure like "Kkhash/ps"
- $info['KHPS'] = $info['KHPS'] * 1000;
- // Format the performance data
- $perfdata = "connections={$info['connections']};;;; difficulty={$info['difficulty']};;;; hashs={$info['KHPS']}hash/ps;;;;";
- if($info['balance'] != '0') {
- // Throw a warning since we have a balance
- echo "WARNING: Balance is at {$info['balance']} | {$perfdata}\n";
- die(1);
- } elseif($info['connections'] == 0) {
- // Something is wrong, the node is up but we have no connections
- echo "CRITICAL: Bitcoin has no connections! | {$perfdata}\n";
- die(2);
- } elseif($info['generate'] == 0) {
- // The node is online but is not generating blocks
- echo "CRITICAL: Bitcoin is not generating blocks! | {$perfdata}\n";
- die(2);
- } else {
- // Everything is good
- echo "OK: Bitcoin is online and generating blocks | {$perfdata}\n";
- die(0);
- }
- /*
- COPYRIGHT
- Copyright 2007 Sergio Vaccaro <sergio@inservibile.org>
- This file is part of JSON-RPC PHP.
- JSON-RPC PHP is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- JSON-RPC PHP is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with JSON-RPC PHP; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- /**
- * The object of this class are generic jsonRPC 1.0 clients
- * http://json-rpc.org/wiki/specification
- *
- * @author sergio <jsonrpcphp@inservibile.org>
- */
- class jsonRPCClient {
- /**
- * Debug state
- *
- * @var boolean
- */
- private $debug;
- /**
- * The server URL
- *
- * @var string
- */
- private $url;
- /**
- * The request id
- *
- * @var integer
- */
- private $id;
- /**
- * If true, notifications are performed instead of requests
- *
- * @var boolean
- */
- private $notification = false;
- /**
- * Takes the connection parameters
- *
- * @param string $url
- * @param boolean $debug
- */
- public function __construct($url,$debug = false) {
- // server URL
- $this->url = $url;
- // proxy
- empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
- // debug state
- empty($debug) ? $this->debug = false : $this->debug = true;
- // message id
- $this->id = 1;
- }
- /**
- * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
- *
- * @param boolean $notification
- */
- public function setRPCNotification($notification) {
- empty($notification) ?
- $this->notification = false
- :
- $this->notification = true;
- }
- /**
- * Performs a jsonRCP request and gets the results as an array
- *
- * @param string $method
- * @param array $params
- * @return array
- */
- public function __call($method,$params) {
- // check
- if (!is_scalar($method)) {
- throw new Exception('Method name has no scalar value');
- }
- // check
- if (is_array($params)) {
- // no keys
- $params = array_values($params);
- } else {
- throw new Exception('Params must be given as array');
- }
- // sets notification or request task
- if ($this->notification) {
- $currentId = NULL;
- } else {
- $currentId = $this->id;
- }
- // prepares the request
- $request = array(
- 'method' => $method,
- 'params' => $params,
- 'id' => $currentId
- );
- $request = json_encode($request);
- $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
- // performs the HTTP POST
- $opts = array ('http' => array (
- 'method' => 'POST',
- 'header' => 'Content-type: application/json',
- 'content' => $request
- ));
- $context = stream_context_create($opts);
- // I added an @ infront of fopen() because I got tired of seeing warnings
- if ($fp = @fopen($this->url, 'r', false, $context)) {
- $response = '';
- while($row = fgets($fp)) {
- $response.= trim($row)."\n";
- }
- $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
- $response = json_decode($response,true);
- } else {
- throw new Exception('Unable to connect to '.$this->url);
- }
- // debug output
- if ($this->debug) {
- echo nl2br($debug);
- }
- // final checks and return
- if (!$this->notification) {
- // check
- if ($response['id'] != $currentId) {
- throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
- }
- if (!is_null($response['error'])) {
- throw new Exception('Request error: '.$response['error']);
- }
- return $response['result'];
- } else {
- return true;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement