2007-04-26 16:27:49 +04:00
/*
ctdb control tool
Copyright ( C ) Andrew Tridgell 2007
2007-05-31 07:50:53 +04:00
This program 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 .
This program is distributed in the hope that it will be useful ,
2007-04-26 16:27:49 +04:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2007-05-31 07:50:53 +04:00
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 this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
2007-04-26 16:27:49 +04:00
*/
# include "includes.h"
# include "lib/events/events.h"
# include "system/filesys.h"
# include "popt.h"
# include "cmdline.h"
2007-05-02 06:43:35 +04:00
# include "../include/ctdb.h"
2007-04-26 16:27:49 +04:00
# include "../include/ctdb_private.h"
2007-05-25 07:05:25 +04:00
# include <arpa/inet.h>
2007-04-26 16:27:49 +04:00
2007-05-29 06:16:59 +04:00
static void usage ( void ) ;
2007-04-26 16:27:49 +04:00
2007-05-29 06:16:59 +04:00
static struct {
int timelimit ;
uint32_t vnn ;
} options ;
# define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
2007-04-26 16:27:49 +04:00
2007-04-27 17:14:36 +04:00
/*
see if a process exists
*/
2007-04-26 16:27:49 +04:00
static int control_process_exists ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
2007-04-26 16:51:41 +04:00
uint32_t vnn , pid ;
2007-04-26 16:27:49 +04:00
int ret ;
2007-04-26 16:51:41 +04:00
if ( argc < 1 ) {
2007-04-26 16:27:49 +04:00
usage ( ) ;
}
2007-04-26 16:51:41 +04:00
if ( sscanf ( argv [ 0 ] , " %u:%u " , & vnn , & pid ) ! = 2 ) {
printf ( " Badly formed vnn:pid \n " ) ;
return - 1 ;
}
2007-04-26 16:27:49 +04:00
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_process_exists ( ctdb , vnn , pid ) ;
2007-04-26 16:27:49 +04:00
if ( ret = = 0 ) {
2007-04-26 16:51:41 +04:00
printf ( " %u:%u exists \n " , vnn , pid ) ;
2007-04-26 16:27:49 +04:00
} else {
2007-04-26 16:51:41 +04:00
printf ( " %u:%u does not exist \n " , vnn , pid ) ;
2007-04-26 16:27:49 +04:00
}
return ret ;
}
2007-04-26 16:51:41 +04:00
/*
2007-05-29 06:16:59 +04:00
display statistics structure
2007-04-26 16:51:41 +04:00
*/
2007-05-29 06:16:59 +04:00
static void show_statistics ( struct ctdb_statistics * s )
2007-04-26 16:51:41 +04:00
{
2007-05-05 02:11:54 +04:00
TALLOC_CTX * tmp_ctx = talloc_new ( NULL ) ;
int i ;
const char * prefix = NULL ;
2007-05-12 15:25:26 +04:00
int preflen = 0 ;
2007-05-05 02:11:54 +04:00
const struct {
const char * name ;
uint32_t offset ;
} fields [ ] = {
2007-05-29 06:16:59 +04:00
# define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
STATISTICS_FIELD ( num_clients ) ,
STATISTICS_FIELD ( frozen ) ,
STATISTICS_FIELD ( recovering ) ,
STATISTICS_FIELD ( client_packets_sent ) ,
STATISTICS_FIELD ( client_packets_recv ) ,
STATISTICS_FIELD ( node_packets_sent ) ,
STATISTICS_FIELD ( node_packets_recv ) ,
STATISTICS_FIELD ( keepalive_packets_sent ) ,
STATISTICS_FIELD ( keepalive_packets_recv ) ,
STATISTICS_FIELD ( node . req_call ) ,
STATISTICS_FIELD ( node . reply_call ) ,
STATISTICS_FIELD ( node . req_dmaster ) ,
STATISTICS_FIELD ( node . reply_dmaster ) ,
STATISTICS_FIELD ( node . reply_error ) ,
STATISTICS_FIELD ( node . req_message ) ,
STATISTICS_FIELD ( node . req_finished ) ,
STATISTICS_FIELD ( node . req_control ) ,
STATISTICS_FIELD ( node . reply_control ) ,
STATISTICS_FIELD ( client . req_call ) ,
STATISTICS_FIELD ( client . req_message ) ,
STATISTICS_FIELD ( client . req_finished ) ,
STATISTICS_FIELD ( client . req_connect_wait ) ,
STATISTICS_FIELD ( client . req_shutdown ) ,
STATISTICS_FIELD ( client . req_control ) ,
STATISTICS_FIELD ( controls . statistics ) ,
STATISTICS_FIELD ( controls . get_config ) ,
STATISTICS_FIELD ( controls . ping ) ,
STATISTICS_FIELD ( controls . attach ) ,
STATISTICS_FIELD ( controls . set_call ) ,
STATISTICS_FIELD ( controls . process_exists ) ,
STATISTICS_FIELD ( controls . traverse_start ) ,
STATISTICS_FIELD ( controls . traverse_all ) ,
STATISTICS_FIELD ( controls . traverse_data ) ,
STATISTICS_FIELD ( controls . update_seqnum ) ,
STATISTICS_FIELD ( controls . enable_seqnum ) ,
STATISTICS_FIELD ( controls . set_seqnum_frequency ) ,
STATISTICS_FIELD ( controls . register_srvid ) ,
STATISTICS_FIELD ( controls . deregister_srvid ) ,
STATISTICS_FIELD ( timeouts . call ) ,
STATISTICS_FIELD ( timeouts . control ) ,
STATISTICS_FIELD ( timeouts . traverse ) ,
STATISTICS_FIELD ( total_calls ) ,
STATISTICS_FIELD ( pending_calls ) ,
STATISTICS_FIELD ( lockwait_calls ) ,
STATISTICS_FIELD ( pending_lockwait_calls ) ,
STATISTICS_FIELD ( memory_used ) ,
STATISTICS_FIELD ( max_hop_count ) ,
2007-05-05 02:11:54 +04:00
} ;
2007-04-26 16:51:41 +04:00
printf ( " CTDB version %u \n " , CTDB_VERSION ) ;
2007-05-05 02:11:54 +04:00
for ( i = 0 ; i < ARRAY_SIZE ( fields ) ; i + + ) {
if ( strchr ( fields [ i ] . name , ' . ' ) ) {
preflen = strcspn ( fields [ i ] . name , " . " ) + 1 ;
if ( ! prefix | | strncmp ( prefix , fields [ i ] . name , preflen ) ! = 0 ) {
prefix = fields [ i ] . name ;
printf ( " %*.*s \n " , preflen - 1 , preflen - 1 , fields [ i ] . name ) ;
}
} else {
preflen = 0 ;
}
printf ( " %*s%-22s%*s%10u \n " ,
preflen ? 4 : 0 , " " ,
fields [ i ] . name + preflen ,
preflen ? 0 : 4 , " " ,
* ( uint32_t * ) ( fields [ i ] . offset + ( uint8_t * ) s ) ) ;
}
printf ( " %-30s %.6f sec \n " , " max_call_latency " , s - > max_call_latency ) ;
printf ( " %-30s %.6f sec \n " , " max_lockwait_latency " , s - > max_lockwait_latency ) ;
talloc_free ( tmp_ctx ) ;
2007-04-26 16:51:41 +04:00
}
2007-04-28 19:13:30 +04:00
/*
2007-05-29 06:16:59 +04:00
display remote ctdb statistics combined from all nodes
2007-04-28 19:13:30 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_statistics_all ( struct ctdb_context * ctdb )
2007-04-28 19:13:30 +04:00
{
int ret , i ;
2007-05-29 06:16:59 +04:00
struct ctdb_statistics statistics ;
2007-04-28 19:42:40 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
2007-04-28 19:42:40 +04:00
CTDB_NO_MEMORY ( ctdb , nodes ) ;
2007-05-29 06:16:59 +04:00
ZERO_STRUCT ( statistics ) ;
2007-04-28 19:13:30 +04:00
2007-04-28 19:42:40 +04:00
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-05-29 06:16:59 +04:00
struct ctdb_statistics s1 ;
2007-04-28 19:13:30 +04:00
int j ;
uint32_t * v1 = ( uint32_t * ) & s1 ;
2007-05-29 06:16:59 +04:00
uint32_t * v2 = ( uint32_t * ) & statistics ;
2007-04-28 19:13:30 +04:00
uint32_t num_ints =
2007-05-29 06:16:59 +04:00
offsetof ( struct ctdb_statistics , __last_counter ) / sizeof ( uint32_t ) ;
ret = ctdb_ctrl_statistics ( ctdb , nodes [ i ] , & s1 ) ;
2007-04-28 19:13:30 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get statistics from node %u \n " , nodes [ i ] ) ;
2007-04-28 19:13:30 +04:00
return ret ;
}
for ( j = 0 ; j < num_ints ; j + + ) {
v2 [ j ] + = v1 [ j ] ;
}
2007-05-29 06:16:59 +04:00
statistics . max_hop_count =
MAX ( statistics . max_hop_count , s1 . max_hop_count ) ;
statistics . max_call_latency =
MAX ( statistics . max_call_latency , s1 . max_call_latency ) ;
statistics . max_lockwait_latency =
MAX ( statistics . max_lockwait_latency , s1 . max_lockwait_latency ) ;
2007-04-28 19:13:30 +04:00
}
2007-04-28 19:42:40 +04:00
talloc_free ( nodes ) ;
2007-05-29 06:16:59 +04:00
printf ( " Gathered statistics for %u nodes \n " , num_nodes ) ;
show_statistics ( & statistics ) ;
2007-04-28 19:13:30 +04:00
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
2007-05-29 06:16:59 +04:00
display remote ctdb statistics
2007-04-27 17:14:36 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_statistics ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-04-26 16:51:41 +04:00
{
int ret ;
2007-05-29 06:16:59 +04:00
struct ctdb_statistics statistics ;
2007-04-26 16:51:41 +04:00
2007-05-29 06:16:59 +04:00
if ( options . vnn = = CTDB_BROADCAST_ALL ) {
return control_statistics_all ( ctdb ) ;
2007-04-28 19:13:30 +04:00
}
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_statistics ( ctdb , options . vnn , & statistics ) ;
2007-04-26 16:51:41 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get statistics from node %u \n " , options . vnn ) ;
2007-04-26 16:51:41 +04:00
return ret ;
}
2007-05-29 06:16:59 +04:00
show_statistics ( & statistics ) ;
2007-04-26 16:51:41 +04:00
return 0 ;
}
2007-04-28 21:13:36 +04:00
/*
2007-05-29 06:16:59 +04:00
reset statistics on all nodes
2007-04-28 21:13:36 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_statistics_reset_all ( struct ctdb_context * ctdb )
2007-04-28 21:13:36 +04:00
{
int ret , i ;
uint32_t * nodes ;
uint32_t num_nodes ;
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
2007-04-28 21:13:36 +04:00
CTDB_NO_MEMORY ( ctdb , nodes ) ;
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-05-29 06:16:59 +04:00
ret = ctdb_statistics_reset ( ctdb , nodes [ i ] ) ;
2007-04-28 21:13:36 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to reset statistics on node %u \n " , nodes [ i ] ) ;
2007-04-28 21:13:36 +04:00
return ret ;
}
}
talloc_free ( nodes ) ;
return 0 ;
}
/*
2007-05-29 06:16:59 +04:00
reset remote ctdb statistics
2007-04-28 21:13:36 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_statistics_reset ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-04-28 21:13:36 +04:00
{
int ret ;
2007-05-29 06:16:59 +04:00
if ( options . vnn = = CTDB_BROADCAST_ALL ) {
return control_statistics_reset_all ( ctdb ) ;
2007-04-28 21:13:36 +04:00
}
2007-05-29 06:16:59 +04:00
ret = ctdb_statistics_reset ( ctdb , options . vnn ) ;
2007-04-28 21:13:36 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to reset statistics on node %u \n " , options . vnn ) ;
2007-04-28 21:13:36 +04:00
return ret ;
}
return 0 ;
}
2007-05-02 04:20:34 +04:00
2007-04-27 17:14:36 +04:00
/*
2007-05-29 06:16:59 +04:00
display remote ctdb status
2007-04-27 17:14:36 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_status ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-04-27 14:56:10 +04:00
{
int i , ret ;
2007-05-03 05:06:24 +04:00
struct ctdb_vnn_map * vnnmap = NULL ;
2007-05-29 06:16:59 +04:00
struct ctdb_node_map * nodemap = NULL ;
uint32_t recmode , recmaster ;
uint32_t myvnn ;
2007-05-30 05:12:50 +04:00
if ( options . vnn = = CTDB_BROADCAST_ALL ) {
uint32_t * nodes ;
uint32_t num_nodes ;
ret = 0 ;
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
CTDB_NO_MEMORY ( ctdb , nodes ) ;
for ( i = 0 ; i < num_nodes ; i + + ) {
options . vnn = nodes [ i ] ;
ret | = control_status ( ctdb , argc , argv ) ;
}
return ret ;
}
myvnn = ctdb_ctrl_getvnn ( ctdb , TIMELIMIT ( ) , options . vnn ) ;
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getnodemap ( ctdb , TIMELIMIT ( ) , options . vnn , ctdb , & nodemap ) ;
if ( ret ! = 0 ) {
printf ( " Unable to get nodemap from node %u \n " , options . vnn ) ;
return ret ;
2007-04-27 14:56:10 +04:00
}
2007-05-29 06:16:59 +04:00
printf ( " Number of nodes:%d \n " , nodemap - > num ) ;
for ( i = 0 ; i < nodemap - > num ; i + + ) {
printf ( " vnn:%d %s%s \n " , nodemap - > nodes [ i ] . vnn ,
nodemap - > nodes [ i ] . flags & NODE_FLAGS_CONNECTED ?
" CONNECTED " : " UNAVAILABLE " ,
nodemap - > nodes [ i ] . vnn = = myvnn ? " (THIS NODE) " : " " ) ;
}
2007-04-27 14:56:10 +04:00
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getvnnmap ( ctdb , TIMELIMIT ( ) , options . vnn , ctdb , & vnnmap ) ;
2007-04-27 14:56:10 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get vnnmap from node %u \n " , options . vnn ) ;
2007-04-27 14:56:10 +04:00
return ret ;
}
printf ( " Generation:%d \n " , vnnmap - > generation ) ;
printf ( " Size:%d \n " , vnnmap - > size ) ;
for ( i = 0 ; i < vnnmap - > size ; i + + ) {
2007-05-29 06:16:59 +04:00
printf ( " hash:%d lmaster:%d \n " , i , vnnmap - > map [ i ] ) ;
2007-05-05 22:31:22 +04:00
}
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getrecmode ( ctdb , TIMELIMIT ( ) , options . vnn , & recmode ) ;
2007-05-05 22:31:22 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get recmode from node %u \n " , options . vnn ) ;
2007-05-05 22:31:22 +04:00
return ret ;
}
2007-05-29 06:16:59 +04:00
printf ( " Recovery mode:%s (%d) \n " , recmode = = CTDB_RECOVERY_NORMAL ? " NORMAL " : " RECOVERY " , recmode ) ;
2007-05-17 04:45:31 +04:00
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getrecmaster ( ctdb , TIMELIMIT ( ) , options . vnn , & recmaster ) ;
2007-05-17 04:45:31 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get recmaster from node %u \n " , options . vnn ) ;
2007-05-17 04:45:31 +04:00
return ret ;
}
2007-05-29 06:16:59 +04:00
printf ( " Recovery master:%d \n " , recmaster ) ;
2007-05-17 04:45:31 +04:00
return 0 ;
}
2007-05-25 07:05:25 +04:00
/*
2007-05-29 06:16:59 +04:00
display pid of a ctdb daemon
2007-05-25 07:05:25 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_getpid ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-05-25 07:05:25 +04:00
{
2007-05-29 06:16:59 +04:00
uint32_t pid ;
2007-05-25 07:05:25 +04:00
int ret ;
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getpid ( ctdb , TIMELIMIT ( ) , options . vnn , & pid ) ;
2007-05-25 07:05:25 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get daemon pid from node %u \n " , options . vnn ) ;
2007-05-25 07:05:25 +04:00
return ret ;
}
2007-05-29 06:16:59 +04:00
printf ( " Pid:%d \n " , pid ) ;
2007-05-25 07:05:25 +04:00
return 0 ;
}
/*
2007-05-29 06:16:59 +04:00
shutdown a daemon
2007-05-25 07:05:25 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_shutdown ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-05-25 07:05:25 +04:00
{
int ret ;
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_shutdown ( ctdb , timeval_current_ofs ( 1 , 0 ) , options . vnn ) ;
2007-05-25 07:05:25 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to shutdown node %u \n " , options . vnn ) ;
2007-05-25 07:05:25 +04:00
return ret ;
}
return 0 ;
}
2007-05-24 02:08:45 +04:00
/*
trigger a recovery
*/
2007-05-29 06:16:59 +04:00
static int control_recover ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-05-24 02:08:45 +04:00
{
int ret ;
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_freeze ( ctdb , TIMELIMIT ( ) , options . vnn ) ;
2007-05-24 02:08:45 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to freeze node \n " ) ;
return ret ;
}
2007-04-29 16:51:56 +04:00
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_setrecmode ( ctdb , TIMELIMIT ( ) , options . vnn , CTDB_RECOVERY_ACTIVE ) ;
2007-04-29 16:51:56 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to set recovery mode \n " ) ;
2007-04-29 16:51:56 +04:00
return ret ;
}
return 0 ;
}
2007-05-21 03:24:34 +04:00
/*
display monitoring mode of a remote node
*/
static int control_getmonmode ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
2007-05-29 06:16:59 +04:00
uint32_t monmode ;
2007-05-21 03:24:34 +04:00
int ret ;
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getmonmode ( ctdb , TIMELIMIT ( ) , options . vnn , & monmode ) ;
2007-05-21 03:24:34 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get monmode from node %u \n " , options . vnn ) ;
2007-05-21 03:24:34 +04:00
return ret ;
}
printf ( " Monitoring mode:%s (%d) \n " , monmode = = CTDB_MONITORING_ACTIVE ? " ACTIVE " : " DISABLED " , monmode ) ;
return 0 ;
}
/*
set the monitoring mode of a remote node
*/
static int control_setmonmode ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
2007-05-29 06:16:59 +04:00
uint32_t monmode ;
2007-05-07 00:51:58 +04:00
int ret ;
if ( argc < 1 ) {
usage ( ) ;
}
2007-05-29 06:16:59 +04:00
monmode = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-05-07 00:51:58 +04:00
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_setmonmode ( ctdb , TIMELIMIT ( ) , options . vnn , monmode ) ;
2007-05-07 00:51:58 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to set monmode on node %u \n " , options . vnn ) ;
2007-05-07 00:51:58 +04:00
return ret ;
}
return 0 ;
}
2007-04-28 23:47:13 +04:00
/*
2007-05-04 06:18:39 +04:00
display remote list of keys / data for a db
2007-04-28 23:47:13 +04:00
*/
2007-04-29 18:58:27 +04:00
static int control_catdb ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-04-28 23:47:13 +04:00
{
2007-05-04 07:25:30 +04:00
const char * db_name ;
2007-05-04 06:18:39 +04:00
struct ctdb_db_context * ctdb_db ;
int ret ;
2007-04-28 23:47:13 +04:00
2007-05-04 06:18:39 +04:00
if ( argc < 1 ) {
2007-04-28 23:47:13 +04:00
usage ( ) ;
}
2007-05-04 07:25:30 +04:00
db_name = argv [ 0 ] ;
ctdb_db = ctdb_attach ( ctdb , db_name ) ;
2007-05-29 06:16:59 +04:00
2007-05-04 06:18:39 +04:00
if ( ctdb_db = = NULL ) {
2007-05-04 07:25:30 +04:00
DEBUG ( 0 , ( " Unable to attach to database '%s' \n " , db_name ) ) ;
2007-05-04 06:18:39 +04:00
return - 1 ;
2007-04-28 23:47:13 +04:00
}
2007-05-04 06:18:39 +04:00
2007-05-29 06:16:59 +04:00
/* traverse and dump the cluster tdb */
ret = ctdb_dump_db ( ctdb_db , stdout ) ;
if ( ret = = - 1 ) {
printf ( " Unable to dump database \n " ) ;
return - 1 ;
2007-05-05 23:53:15 +04:00
}
2007-05-04 07:25:30 +04:00
talloc_free ( ctdb_db ) ;
2007-05-04 06:18:39 +04:00
printf ( " Dumped %d records \n " , ret ) ;
2007-04-28 23:47:13 +04:00
return 0 ;
}
2007-04-29 18:58:27 +04:00
2007-04-28 14:00:50 +04:00
/*
display a list of the databases on a remote ctdb
*/
static int control_getdbmap ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
int i , ret ;
2007-05-03 07:07:34 +04:00
struct ctdb_dbid_map * dbmap = NULL ;
2007-04-28 14:00:50 +04:00
2007-05-29 06:16:59 +04:00
ret = ctdb_ctrl_getdbmap ( ctdb , TIMELIMIT ( ) , options . vnn , ctdb , & dbmap ) ;
2007-04-28 14:00:50 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to get dbids from node %u \n " , options . vnn ) ;
2007-04-28 14:00:50 +04:00
return ret ;
}
2007-05-03 07:07:34 +04:00
printf ( " Number of databases:%d \n " , dbmap - > num ) ;
for ( i = 0 ; i < dbmap - > num ; i + + ) {
2007-04-28 14:00:50 +04:00
const char * path ;
2007-05-05 23:53:15 +04:00
const char * name ;
2007-04-28 14:00:50 +04:00
2007-05-29 06:16:59 +04:00
ctdb_ctrl_getdbpath ( ctdb , TIMELIMIT ( ) , options . vnn , dbmap - > dbids [ i ] , ctdb , & path ) ;
ctdb_ctrl_getdbname ( ctdb , TIMELIMIT ( ) , options . vnn , dbmap - > dbids [ i ] , ctdb , & name ) ;
2007-05-05 23:53:15 +04:00
printf ( " dbid:0x%08x name:%s path:%s \n " , dbmap - > dbids [ i ] , name , path ) ;
2007-04-28 14:00:50 +04:00
}
2007-05-02 04:20:34 +04:00
2007-04-28 14:00:50 +04:00
return 0 ;
}
2007-04-28 14:40:26 +04:00
/*
2007-05-29 06:16:59 +04:00
ping a node
2007-04-27 17:14:36 +04:00
*/
2007-04-26 21:27:07 +04:00
static int control_ping ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
int ret , i ;
2007-04-28 19:42:40 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
2007-04-26 21:27:07 +04:00
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
2007-04-28 19:42:40 +04:00
CTDB_NO_MEMORY ( ctdb , nodes ) ;
2007-04-26 21:27:07 +04:00
2007-04-28 19:42:40 +04:00
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-04-26 21:27:07 +04:00
struct timeval tv = timeval_current ( ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_ping ( ctdb , nodes [ i ] ) ;
2007-04-28 17:15:21 +04:00
if ( ret = = - 1 ) {
2007-04-28 19:42:40 +04:00
printf ( " Unable to get ping response from node %u \n " , nodes [ i ] ) ;
2007-04-26 21:27:07 +04:00
} else {
2007-04-28 17:15:21 +04:00
printf ( " response from %u time=%.6f sec (%d clients) \n " ,
2007-04-28 19:42:40 +04:00
nodes [ i ] , timeval_elapsed ( & tv ) , ret ) ;
2007-04-26 21:27:07 +04:00
}
}
2007-04-28 19:42:40 +04:00
talloc_free ( nodes ) ;
2007-04-26 21:27:07 +04:00
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
2007-05-29 06:16:59 +04:00
display debug level on a node
2007-04-27 17:14:36 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_getdebug ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-04-27 17:14:36 +04:00
{
int ret , i ;
2007-04-28 19:42:40 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
2007-05-29 06:16:59 +04:00
uint32_t level ;
2007-04-28 19:42:40 +04:00
2007-05-29 06:16:59 +04:00
if ( options . vnn ! = CTDB_BROADCAST_ALL ) {
ret = ctdb_ctrl_get_debuglevel ( ctdb , options . vnn , & level ) ;
if ( ret ! = 0 ) {
printf ( " Unable to get debuglevel response from node %u \n " ,
options . vnn ) ;
} else {
printf ( " Node %u is at debug level %u \n " , options . vnn , level ) ;
}
return 0 ;
}
2007-04-27 17:14:36 +04:00
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
CTDB_NO_MEMORY ( ctdb , nodes ) ;
2007-04-28 19:42:40 +04:00
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_get_debuglevel ( ctdb , nodes [ i ] , & level ) ;
2007-04-27 17:14:36 +04:00
if ( ret ! = 0 ) {
2007-04-28 19:42:40 +04:00
printf ( " Unable to get debuglevel response from node %u \n " ,
nodes [ i ] ) ;
2007-04-27 17:14:36 +04:00
} else {
2007-04-28 19:42:40 +04:00
printf ( " Node %u is at debug level %u \n " , nodes [ i ] , level ) ;
2007-04-27 17:14:36 +04:00
}
}
2007-04-28 19:42:40 +04:00
talloc_free ( nodes ) ;
2007-04-27 17:14:36 +04:00
return 0 ;
}
2007-05-29 06:16:59 +04:00
2007-04-27 17:14:36 +04:00
/*
2007-05-29 06:16:59 +04:00
set debug level on a node or all nodes
2007-04-27 17:14:36 +04:00
*/
2007-05-29 06:16:59 +04:00
static int control_setdebug ( struct ctdb_context * ctdb , int argc , const char * * argv )
2007-04-27 17:14:36 +04:00
{
int ret ;
2007-05-29 06:16:59 +04:00
uint32_t level , i ;
2007-04-28 19:42:40 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
2007-04-27 17:14:36 +04:00
2007-05-29 06:16:59 +04:00
if ( argc < 1 ) {
2007-04-27 17:14:36 +04:00
usage ( ) ;
}
2007-05-29 06:16:59 +04:00
level = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-04-27 17:14:36 +04:00
2007-05-29 06:16:59 +04:00
if ( options . vnn ! = CTDB_BROADCAST_ALL ) {
ret = ctdb_ctrl_set_debuglevel ( ctdb , options . vnn , level ) ;
2007-04-28 19:42:40 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to set debug level on node %u \n " , options . vnn ) ;
2007-04-28 19:13:30 +04:00
}
return 0 ;
}
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
2007-04-28 19:42:40 +04:00
CTDB_NO_MEMORY ( ctdb , nodes ) ;
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_set_debuglevel ( ctdb , nodes [ i ] , level ) ;
2007-04-28 19:42:40 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to set debug level on node %u \n " , nodes [ i ] ) ;
break ;
}
2007-04-27 17:14:36 +04:00
}
2007-04-28 19:42:40 +04:00
talloc_free ( nodes ) ;
2007-04-27 17:14:36 +04:00
return 0 ;
}
2007-04-30 17:54:06 +04:00
2007-05-12 09:15:27 +04:00
/*
freeze a node
*/
static int control_freeze ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
2007-05-12 09:44:56 +04:00
int ret = 0 , count = 0 ;
2007-05-29 06:16:59 +04:00
uint32_t i ;
2007-05-12 09:15:27 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
2007-05-29 06:16:59 +04:00
if ( options . vnn ! = CTDB_BROADCAST_ALL ) {
ret = ctdb_ctrl_freeze ( ctdb , TIMELIMIT ( ) , options . vnn ) ;
2007-05-12 09:15:27 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to freeze node %u \n " , options . vnn ) ;
2007-05-12 09:15:27 +04:00
}
return 0 ;
}
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
2007-05-12 09:15:27 +04:00
CTDB_NO_MEMORY ( ctdb , nodes ) ;
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-05-29 06:16:59 +04:00
int res = ctdb_ctrl_freeze ( ctdb , TIMELIMIT ( ) , nodes [ i ] ) ;
2007-05-12 09:15:27 +04:00
if ( res ! = 0 ) {
printf ( " Warning: Unable to freeze node %u \n " , nodes [ i ] ) ;
2007-05-12 09:44:56 +04:00
} else {
count + + ;
2007-05-12 09:15:27 +04:00
}
ret | = res ;
}
2007-05-12 09:44:56 +04:00
printf ( " Froze %u nodes \n " , count ) ;
2007-05-12 09:15:27 +04:00
talloc_free ( nodes ) ;
return 0 ;
}
/*
thaw a node
*/
static int control_thaw ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
2007-05-12 09:44:56 +04:00
int ret = 0 , count = 0 ;
2007-05-29 06:16:59 +04:00
uint32_t i ;
2007-05-12 09:15:27 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
2007-05-29 06:16:59 +04:00
if ( options . vnn ! = CTDB_BROADCAST_ALL ) {
ret = ctdb_ctrl_thaw ( ctdb , TIMELIMIT ( ) , options . vnn ) ;
2007-05-12 09:15:27 +04:00
if ( ret ! = 0 ) {
2007-05-29 06:16:59 +04:00
printf ( " Unable to thaw node %u \n " , options . vnn ) ;
2007-05-12 09:15:27 +04:00
}
return 0 ;
}
2007-05-29 06:16:59 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , TIMELIMIT ( ) , ctdb , & num_nodes ) ;
2007-05-12 09:15:27 +04:00
CTDB_NO_MEMORY ( ctdb , nodes ) ;
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-05-29 06:16:59 +04:00
int res = ctdb_ctrl_thaw ( ctdb , TIMELIMIT ( ) , nodes [ i ] ) ;
2007-05-12 09:15:27 +04:00
if ( res ! = 0 ) {
printf ( " Warning: Unable to thaw node %u \n " , nodes [ i ] ) ;
2007-05-12 09:44:56 +04:00
} else {
count + + ;
2007-05-12 09:15:27 +04:00
}
ret | = res ;
}
2007-05-12 09:44:56 +04:00
printf ( " Thawed %u nodes \n " , count ) ;
2007-05-12 09:15:27 +04:00
talloc_free ( nodes ) ;
return 0 ;
}
2007-04-30 17:54:06 +04:00
/*
attach to a database
*/
static int control_attach ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
const char * db_name ;
struct ctdb_db_context * ctdb_db ;
2007-05-29 06:16:59 +04:00
2007-04-30 17:54:06 +04:00
if ( argc < 1 ) {
usage ( ) ;
}
db_name = argv [ 0 ] ;
ctdb_db = ctdb_attach ( ctdb , db_name ) ;
if ( ctdb_db = = NULL ) {
DEBUG ( 0 , ( " Unable to attach to database '%s' \n " , db_name ) ) ;
return - 1 ;
}
return 0 ;
}
2007-05-05 05:03:10 +04:00
/*
dump memory usage
*/
static int control_dumpmemory ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
2007-05-29 06:16:59 +04:00
return ctdb_control ( ctdb , options . vnn , 0 , CTDB_CONTROL_DUMP_MEMORY ,
CTDB_CTRL_FLAG_NOREPLY , tdb_null , NULL , NULL , NULL , NULL , NULL ) ;
}
static const struct {
const char * name ;
int ( * fn ) ( struct ctdb_context * , int , const char * * ) ;
const char * msg ;
const char * args ;
} ctdb_commands [ ] = {
{ " status " , control_status , " show node status " } ,
{ " ping " , control_ping , " ping all nodes " } ,
{ " statistics " , control_statistics , " show statistics " } ,
{ " statisticsreset " , control_statistics_reset , " reset statistics " } ,
{ " process-exists " , control_process_exists , " check if a process exists on a node " , " <pid> " } ,
{ " getdbmap " , control_getdbmap , " show the database map " } ,
{ " catdb " , control_catdb , " dump a database " , " <dbname> " } ,
{ " getmonmode " , control_getmonmode , " show monitoring mode " } ,
{ " setmonmode " , control_setmonmode , " set monitoring mode " } ,
{ " setdebug " , control_setdebug , " set debug level " , " <debuglevel> " } ,
{ " getdebug " , control_getdebug , " get debug level " } ,
{ " attach " , control_attach , " attach to a database " , " <dbname> " } ,
{ " dumpmemory " , control_dumpmemory , " dump memory map to logs " } ,
{ " getpid " , control_getpid , " get ctdbd process ID " } ,
{ " shutdown " , control_shutdown , " shutdown ctdbd " } ,
{ " recover " , control_recover , " force recovery " } ,
{ " freeze " , control_freeze , " freeze all databases " } ,
{ " thaw " , control_thaw , " thaw all databases " } ,
} ;
2007-05-05 05:03:10 +04:00
2007-05-29 06:16:59 +04:00
/*
show usage message
*/
static void usage ( void )
{
int i ;
printf (
" Usage: ctdb [options] <control> \n " \
" Options: \n " \
" -n <node> choose node number, or 'all' (defaults to local node) \n "
" -t <timelimit> set timelimit for control in seconds (default %u) \n " , options . timelimit ) ;
printf ( " Controls: \n " ) ;
for ( i = 0 ; i < ARRAY_SIZE ( ctdb_commands ) ; i + + ) {
printf ( " %-15s %-20s %s \n " ,
ctdb_commands [ i ] . name ,
ctdb_commands [ i ] . args ? ctdb_commands [ i ] . args : " " ,
ctdb_commands [ i ] . msg ) ;
2007-05-05 05:03:10 +04:00
}
2007-05-29 06:16:59 +04:00
exit ( 1 ) ;
2007-05-05 05:03:10 +04:00
}
2007-05-29 06:16:59 +04:00
2007-04-26 16:27:49 +04:00
/*
main program
*/
int main ( int argc , const char * argv [ ] )
{
struct ctdb_context * ctdb ;
2007-05-29 06:16:59 +04:00
char * nodestring = NULL ;
2007-04-26 16:27:49 +04:00
struct poptOption popt_options [ ] = {
POPT_AUTOHELP
POPT_CTDB_CMDLINE
2007-05-29 06:16:59 +04:00
{ " timelimit " , ' t ' , POPT_ARG_INT , & options . timelimit , 0 , " timelimit " , " integer " } ,
{ " node " , ' n ' , POPT_ARG_STRING , & nodestring , 0 , " node " , " integer|all " } ,
2007-04-26 16:27:49 +04:00
POPT_TABLEEND
} ;
int opt ;
const char * * extra_argv ;
int extra_argc = 0 ;
2007-05-29 10:02:02 +04:00
int ret = - 1 , i ;
2007-04-26 16:27:49 +04:00
poptContext pc ;
struct event_context * ev ;
const char * control ;
2007-05-29 06:16:59 +04:00
/* set some defaults */
options . timelimit = 3 ;
options . vnn = CTDB_CURRENT_NODE ;
2007-04-26 16:27:49 +04:00
pc = poptGetContext ( argv [ 0 ] , argc , argv , popt_options , POPT_CONTEXT_KEEP_FIRST ) ;
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
switch ( opt ) {
default :
fprintf ( stderr , " Invalid option %s: %s \n " ,
poptBadOption ( pc , 0 ) , poptStrerror ( opt ) ) ;
exit ( 1 ) ;
}
}
/* setup the remaining options for the main program to use */
extra_argv = poptGetArgs ( pc ) ;
if ( extra_argv ) {
extra_argv + + ;
while ( extra_argv [ extra_argc ] ) extra_argc + + ;
}
if ( extra_argc < 1 ) {
usage ( ) ;
}
2007-05-29 06:16:59 +04:00
/* setup the node number to contact */
if ( nodestring ! = NULL ) {
if ( strcmp ( nodestring , " all " ) = = 0 ) {
options . vnn = CTDB_BROADCAST_ALL ;
} else {
options . vnn = strtoul ( nodestring , NULL , 0 ) ;
}
}
2007-04-26 16:27:49 +04:00
control = extra_argv [ 0 ] ;
ev = event_context_init ( NULL ) ;
/* initialise ctdb */
ctdb = ctdb_cmdline_client ( ev ) ;
if ( ctdb = = NULL ) {
printf ( " Failed to init ctdb \n " ) ;
exit ( 1 ) ;
}
2007-05-29 06:16:59 +04:00
for ( i = 0 ; i < ARRAY_SIZE ( ctdb_commands ) ; i + + ) {
if ( strcmp ( control , ctdb_commands [ i ] . name ) = = 0 ) {
ret = ctdb_commands [ i ] . fn ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-05-02 07:34:55 +04:00
break ;
}
}
2007-05-29 06:16:59 +04:00
if ( i = = ARRAY_SIZE ( ctdb_commands ) ) {
2007-04-26 16:27:49 +04:00
printf ( " Unknown control '%s' \n " , control ) ;
exit ( 1 ) ;
}
return ret ;
}