2007-04-26 16:27:49 +04:00
/*
ctdb control tool
Copyright ( C ) Andrew Tridgell 2007
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2 of the License , or ( at your option ) any later version .
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include "includes.h"
# include "lib/events/events.h"
# include "system/filesys.h"
# include "popt.h"
# include "cmdline.h"
# include "../include/ctdb_private.h"
/*
show usage message
*/
static void usage ( void )
{
printf ( " Usage: ctdb_control [options] <control> \n " ) ;
2007-04-26 16:51:41 +04:00
printf ( " \n Controls: \n " ) ;
2007-04-26 21:27:07 +04:00
printf ( " ping \n " ) ;
2007-04-27 17:14:36 +04:00
printf ( " process-exists <vnn:pid> see if a process exists \n " ) ;
2007-04-28 19:13:30 +04:00
printf ( " status <vnn|all> show ctdb status on a node \n " ) ;
2007-04-28 21:13:36 +04:00
printf ( " statusreset <vnn|all> reset status on a node \n " ) ;
2007-04-28 19:13:30 +04:00
printf ( " debug <vnn|all> <level> set ctdb debug level on a node \n " ) ;
2007-04-27 17:14:36 +04:00
printf ( " debuglevel display ctdb debug levels \n " ) ;
printf ( " getvnnmap <vnn> display ctdb vnnmap \n " ) ;
2007-04-27 16:08:12 +04:00
printf ( " setvnnmap <vnn> <generation> <numslots> <lmaster>* \n " ) ;
2007-04-28 14:00:50 +04:00
printf ( " getdbmap <vnn> lists databases on a node \n " ) ;
2007-04-28 14:40:26 +04:00
printf ( " getnodemap <vnn> lists nodes known to a ctdb daemon \n " ) ;
2007-04-29 18:58:27 +04:00
printf ( " catdb <vnn> <dbid> lists all keys in a remote tdb \n " ) ;
printf ( " cpdb <fromvnn> <tovnn> <dbid> lists all keys in a remote tdb \n " ) ;
2007-04-29 12:34:11 +04:00
printf ( " setdmaster <vnn> <dbid> <dmaster> sets new dmaster for all records in the database \n " ) ;
2007-04-29 12:48:46 +04:00
printf ( " cleardb <vnn> <dbid> deletes all records in a db \n " ) ;
2007-04-29 16:51:56 +04:00
printf ( " getrecmode <vnn> get recovery mode \n " ) ;
printf ( " setrecmode <vnn> <mode> set recovery mode \n " ) ;
2007-05-02 04:37:43 +04:00
printf ( " recover <vnn> recover the cluster \n " ) ;
2007-04-26 16:27:49 +04:00
exit ( 1 ) ;
}
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
/*
display status structure
*/
static void show_status ( struct ctdb_status * s )
{
printf ( " CTDB version %u \n " , CTDB_VERSION ) ;
printf ( " client_packets_sent %u \n " , s - > client_packets_sent ) ;
printf ( " client_packets_recv %u \n " , s - > client_packets_recv ) ;
printf ( " req_call %u \n " , s - > client . req_call ) ;
printf ( " req_message %u \n " , s - > client . req_message ) ;
printf ( " req_finished %u \n " , s - > client . req_finished ) ;
printf ( " req_register %u \n " , s - > client . req_register ) ;
printf ( " req_connect_wait %u \n " , s - > client . req_connect_wait ) ;
printf ( " req_shutdown %u \n " , s - > client . req_shutdown ) ;
printf ( " req_control %u \n " , s - > client . req_control ) ;
printf ( " node_packets_sent %u \n " , s - > node_packets_sent ) ;
printf ( " node_packets_recv %u \n " , s - > node_packets_recv ) ;
printf ( " req_call %u \n " , s - > count . req_call ) ;
printf ( " reply_call %u \n " , s - > count . reply_call ) ;
printf ( " req_dmaster %u \n " , s - > count . req_dmaster ) ;
printf ( " reply_dmaster %u \n " , s - > count . reply_dmaster ) ;
printf ( " reply_error %u \n " , s - > count . reply_error ) ;
printf ( " req_message %u \n " , s - > count . req_message ) ;
printf ( " req_finished %u \n " , s - > count . req_finished ) ;
printf ( " total_calls %u \n " , s - > total_calls ) ;
printf ( " pending_calls %u \n " , s - > pending_calls ) ;
printf ( " lockwait_calls %u \n " , s - > lockwait_calls ) ;
printf ( " pending_lockwait_calls %u \n " , s - > pending_lockwait_calls ) ;
2007-05-01 07:25:02 +04:00
printf ( " max_hop_count %u \n " , s - > max_hop_count ) ;
2007-04-26 16:51:41 +04:00
printf ( " max_call_latency %.6f sec \n " , s - > max_call_latency ) ;
printf ( " max_lockwait_latency %.6f sec \n " , s - > max_lockwait_latency ) ;
}
2007-04-28 19:13:30 +04:00
/*
display remote ctdb status combined from all nodes
*/
static int control_status_all ( struct ctdb_context * ctdb )
{
int ret , i ;
struct ctdb_status status ;
2007-04-28 19:42:40 +04:00
uint32_t * nodes ;
uint32_t num_nodes ;
nodes = ctdb_get_connected_nodes ( ctdb , ctdb , & num_nodes ) ;
CTDB_NO_MEMORY ( ctdb , nodes ) ;
2007-04-28 19:13:30 +04:00
ZERO_STRUCT ( status ) ;
2007-04-28 19:42:40 +04:00
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-04-28 19:13:30 +04:00
struct ctdb_status s1 ;
int j ;
uint32_t * v1 = ( uint32_t * ) & s1 ;
uint32_t * v2 = ( uint32_t * ) & status ;
uint32_t num_ints =
2007-04-28 19:46:37 +04:00
offsetof ( struct ctdb_status , __last_counter ) / sizeof ( uint32_t ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_status ( ctdb , nodes [ i ] , & s1 ) ;
2007-04-28 19:13:30 +04:00
if ( ret ! = 0 ) {
2007-04-28 19:42:40 +04:00
printf ( " Unable to get status 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-01 07:25:02 +04:00
status . max_hop_count =
MAX ( status . max_hop_count , s1 . max_hop_count ) ;
2007-04-28 19:13:30 +04:00
status . max_call_latency =
MAX ( status . max_call_latency , s1 . max_call_latency ) ;
status . max_lockwait_latency =
MAX ( status . max_lockwait_latency , s1 . max_lockwait_latency ) ;
}
2007-04-28 19:42:40 +04:00
talloc_free ( nodes ) ;
printf ( " Gathered status for %u nodes \n " , num_nodes ) ;
2007-04-28 19:13:30 +04:00
show_status ( & status ) ;
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
display remote ctdb status
*/
2007-04-26 16:51:41 +04:00
static int control_status ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn ;
int ret ;
struct ctdb_status status ;
if ( argc < 1 ) {
usage ( ) ;
}
2007-04-28 19:13:30 +04:00
if ( strcmp ( argv [ 0 ] , " all " ) = = 0 ) {
return control_status_all ( ctdb ) ;
}
2007-04-26 16:51:41 +04:00
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_status ( ctdb , vnn , & status ) ;
2007-04-26 16:51:41 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to get status from node %u \n " , vnn ) ;
return ret ;
}
show_status ( & status ) ;
return 0 ;
}
2007-04-28 21:13:36 +04:00
/*
reset status on all nodes
*/
static int control_status_reset_all ( struct ctdb_context * ctdb )
{
int ret , i ;
uint32_t * nodes ;
uint32_t num_nodes ;
nodes = ctdb_get_connected_nodes ( ctdb , ctdb , & num_nodes ) ;
CTDB_NO_MEMORY ( ctdb , nodes ) ;
for ( i = 0 ; i < num_nodes ; i + + ) {
ret = ctdb_status_reset ( ctdb , nodes [ i ] ) ;
if ( ret ! = 0 ) {
printf ( " Unable to reset status on node %u \n " , nodes [ i ] ) ;
return ret ;
}
}
talloc_free ( nodes ) ;
return 0 ;
}
/*
reset remote ctdb status
*/
static int control_status_reset ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn ;
int ret ;
if ( argc < 1 ) {
usage ( ) ;
}
if ( strcmp ( argv [ 0 ] , " all " ) = = 0 ) {
return control_status_reset_all ( ctdb ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
ret = ctdb_status_reset ( ctdb , vnn ) ;
if ( ret ! = 0 ) {
printf ( " Unable to reset status on node %u \n " , vnn ) ;
return ret ;
}
return 0 ;
}
2007-05-02 04:20:34 +04:00
/*
perform a samba3 style recovery
*/
static int control_recover ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn , num_nodes , generation , dmaster ;
struct ctdb_vnn_map vnnmap ;
struct ctdb_node_map nodemap ;
int i , j , ret ;
struct ctdb_dbid_map dbmap ;
2007-05-02 04:37:43 +04:00
if ( argc < 1 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
printf ( " recover ctdb from node %d \n " , vnn ) ;
2007-05-02 04:20:34 +04:00
/* 1: find a list of all nodes */
printf ( " \n 1: fetching list of nodes \n " ) ;
ret = ctdb_ctrl_getnodemap ( ctdb , vnn , ctdb , & nodemap ) ;
if ( ret ! = 0 ) {
printf ( " Unable to get nodemap from node %u \n " , vnn ) ;
return ret ;
}
/* 2: count the active nodes */
printf ( " \n 2: count number of active nodes \n " ) ;
num_nodes = 0 ;
for ( i = 0 ; i < nodemap . num ; i + + ) {
if ( nodemap . nodes [ i ] . flags & NODE_FLAGS_CONNECTED ) {
num_nodes + + ;
}
}
printf ( " number of active nodes:%d \n " , num_nodes ) ;
/* 3: go to all active nodes and activate recovery mode */
printf ( " \n 3: set recovery mode for all active nodes \n " ) ;
for ( j = 0 ; j < nodemap . num ; j + + ) {
/* dont change it for nodes that are unavailable */
if ( ! ( nodemap . nodes [ j ] . flags & NODE_FLAGS_CONNECTED ) ) {
continue ;
}
printf ( " setting node %d to recovery mode \n " , nodemap . nodes [ j ] . vnn ) ;
ret = ctdb_ctrl_setrecmode ( ctdb , nodemap . nodes [ j ] . vnn , CTDB_RECOVERY_ACTIVE ) ;
if ( ret ! = 0 ) {
printf ( " Unable to set recmode on node %u \n " , nodemap . nodes [ j ] . vnn ) ;
return ret ;
}
}
/* 4: get a list of all databases */
printf ( " \n 4: getting list of databases to recover \n " ) ;
ret = ctdb_ctrl_getdbmap ( ctdb , vnn , ctdb , & dbmap ) ;
if ( ret ! = 0 ) {
printf ( " Unable to get dbids from node %u \n " , vnn ) ;
return ret ;
}
for ( i = 0 ; i < dbmap . num ; i + + ) {
const char * path ;
ctdb_ctrl_getdbpath ( ctdb , dbmap . dbids [ i ] , ctdb , & path ) ;
printf ( " dbid:0x%08x path:%s \n " , dbmap . dbids [ i ] , path ) ;
}
/* 5: pull all records from all other nodes across to this node
( this merges based on rsn internally )
*/
printf ( " \n 5: merge all records from remote nodes \n " ) ;
for ( i = 0 ; i < dbmap . num ; i + + ) {
printf ( " recovering database 0x%08x \n " , dbmap . dbids [ i ] ) ;
for ( j = 0 ; j < nodemap . num ; j + + ) {
/* we dont need to merge with ourselves */
if ( nodemap . nodes [ j ] . vnn = = vnn ) {
continue ;
}
/* dont merge from nodes that are unavailable */
if ( ! ( nodemap . nodes [ j ] . flags & NODE_FLAGS_CONNECTED ) ) {
continue ;
}
printf ( " merging all records from node %d for database 0x%08x \n " , nodemap . nodes [ j ] . vnn , dbmap . dbids [ i ] ) ;
ret = ctdb_ctrl_copydb ( ctdb , nodemap . nodes [ j ] . vnn , vnn , dbmap . dbids [ i ] , CTDB_LMASTER_ANY , ctdb ) ;
if ( ret ! = 0 ) {
printf ( " Unable to copy db from node %u to node %u \n " , nodemap . nodes [ j ] . vnn , vnn ) ;
return ret ;
}
}
}
/* 6: update dmaster to point to this node for all databases/nodes */
printf ( " \n 6: repoint dmaster to the recovery node \n " ) ;
dmaster = vnn ;
printf ( " new dmaster is %d \n " , dmaster ) ;
for ( i = 0 ; i < dbmap . num ; i + + ) {
for ( j = 0 ; j < nodemap . num ; j + + ) {
/* dont repoint nodes that are unavailable */
if ( ! ( nodemap . nodes [ j ] . flags & NODE_FLAGS_CONNECTED ) ) {
continue ;
}
printf ( " setting dmaster to %d for node %d db 0x%08x \n " , dmaster , nodemap . nodes [ j ] . vnn , dbmap . dbids [ i ] ) ;
ret = ctdb_ctrl_setdmaster ( ctdb , nodemap . nodes [ j ] . vnn , ctdb , dbmap . dbids [ i ] , dmaster ) ;
if ( ret ! = 0 ) {
printf ( " Unable to set dmaster for node %u db:0x%08x \n " , nodemap . nodes [ j ] . vnn , dbmap . dbids [ i ] ) ;
return ret ;
}
}
}
/* 7: push all records out to the nodes again */
printf ( " \n 7: push all records to remote nodes \n " ) ;
for ( i = 0 ; i < dbmap . num ; i + + ) {
printf ( " distributing new database 0x%08x \n " , dbmap . dbids [ i ] ) ;
for ( j = 0 ; j < nodemap . num ; j + + ) {
/* we dont need to push to ourselves */
if ( nodemap . nodes [ j ] . vnn = = vnn ) {
continue ;
}
/* dont push to nodes that are unavailable */
if ( ! ( nodemap . nodes [ j ] . flags & NODE_FLAGS_CONNECTED ) ) {
continue ;
}
printf ( " pushing all records to node %d for database 0x%08x \n " , nodemap . nodes [ j ] . vnn , dbmap . dbids [ i ] ) ;
ret = ctdb_ctrl_copydb ( ctdb , vnn , nodemap . nodes [ j ] . vnn , dbmap . dbids [ i ] , CTDB_LMASTER_ANY , ctdb ) ;
if ( ret ! = 0 ) {
printf ( " Unable to copy db from node %u to node %u \n " , vnn , nodemap . nodes [ j ] . vnn ) ;
return ret ;
}
}
}
/* 8: build a new vnn map */
printf ( " \n 8: build a new vnn map with a new generation id \n " ) ;
generation = random ( ) ;
vnnmap . generation = generation ;
vnnmap . size = num_nodes ;
vnnmap . map = talloc_array ( ctdb , uint32_t , num_nodes ) ;
for ( i = j = 0 ; i < nodemap . num ; i + + ) {
if ( nodemap . nodes [ i ] . flags & NODE_FLAGS_CONNECTED ) {
vnnmap . map [ j + + ] = nodemap . nodes [ i ] . vnn ;
}
}
printf ( " Generation:%d \n " , vnnmap . generation ) ;
printf ( " Size:%d \n " , vnnmap . size ) ;
for ( i = 0 ; i < vnnmap . size ; i + + ) {
printf ( " hash:%d lmaster:%d \n " , i , vnnmap . map [ i ] ) ;
}
/* 9: push the new vnn map out to all the nodes */
printf ( " \n 9: distribute the new vnn map \n " ) ;
for ( j = 0 ; j < nodemap . num ; j + + ) {
/* dont push to nodes that are unavailable */
if ( ! ( nodemap . nodes [ j ] . flags & NODE_FLAGS_CONNECTED ) ) {
continue ;
}
printf ( " setting new vnn map on node %d \n " , nodemap . nodes [ j ] . vnn ) ;
ret = ctdb_ctrl_setvnnmap ( ctdb , nodemap . nodes [ j ] . vnn , ctdb , & vnnmap ) ;
if ( ret ! = 0 ) {
printf ( " Unable to set vnnmap for node %u \n " , vnn ) ;
return ret ;
}
}
/* 10: disable recovery mode */
printf ( " \n 10: restore recovery mode back to normal \n " ) ;
for ( j = 0 ; j < nodemap . num ; j + + ) {
/* dont push to nodes that are unavailable */
if ( ! ( nodemap . nodes [ j ] . flags & NODE_FLAGS_CONNECTED ) ) {
continue ;
}
printf ( " changing recovery mode back to normal for node %d \n " , nodemap . nodes [ j ] . vnn ) ;
ret = ctdb_ctrl_setrecmode ( ctdb , nodemap . nodes [ j ] . vnn , CTDB_RECOVERY_NORMAL ) ;
if ( ret ! = 0 ) {
printf ( " Unable to set recmode on node %u \n " , nodemap . nodes [ j ] . vnn ) ;
return ret ;
}
}
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
display remote ctdb vnn map
*/
2007-04-27 14:56:10 +04:00
static int control_getvnnmap ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn ;
int i , ret ;
struct ctdb_vnn_map * vnnmap ;
if ( argc < 1 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
vnnmap = talloc_zero ( ctdb , struct ctdb_vnn_map ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_getvnnmap ( ctdb , vnn , vnnmap ) ;
2007-04-27 14:56:10 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to get vnnmap from node %u \n " , vnn ) ;
return ret ;
}
printf ( " Generation:%d \n " , vnnmap - > generation ) ;
printf ( " Size:%d \n " , vnnmap - > size ) ;
for ( i = 0 ; i < vnnmap - > size ; i + + ) {
printf ( " hash:%d lmaster:%d \n " , i , vnnmap - > map [ i ] ) ;
}
return 0 ;
}
2007-04-26 21:27:07 +04:00
2007-04-29 16:51:56 +04:00
/*
display recovery mode of a remote node
*/
static int control_getrecmode ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn , recmode ;
int ret ;
if ( argc < 1 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_getrecmode ( ctdb , vnn , & recmode ) ;
2007-04-29 16:51:56 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to get recmode from node %u \n " , vnn ) ;
return ret ;
}
printf ( " Recovery mode:%s (%d) \n " , recmode = = CTDB_RECOVERY_NORMAL ? " NORMAL " : " RECOVERY " , recmode ) ;
return 0 ;
}
/*
set recovery mode of a remote node
*/
static int control_setrecmode ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn , recmode ;
int ret ;
if ( argc < 2 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
recmode = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_setrecmode ( ctdb , vnn , recmode ) ;
2007-04-29 16:51:56 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to set recmode on node %u \n " , vnn ) ;
return ret ;
}
return 0 ;
}
2007-04-28 23:47:13 +04:00
/*
display remote list of keys for a tdb
*/
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
{
uint32_t vnn , dbid ;
int i , j , ret ;
struct ctdb_key_list keys ;
TALLOC_CTX * mem_ctx ;
if ( argc < 2 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
dbid = strtoul ( argv [ 1 ] , NULL , 0 ) ;
mem_ctx = talloc_new ( ctdb ) ;
2007-05-02 04:20:34 +04:00
ret = ctdb_ctrl_pulldb ( ctdb , vnn , dbid , CTDB_LMASTER_ANY , mem_ctx , & keys ) ;
2007-04-28 23:47:13 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to get keys from node %u \n " , vnn ) ;
return ret ;
}
2007-04-29 18:58:27 +04:00
printf ( " Number of keys:%d in dbid:0x%08x \n " , keys . num , keys . dbid ) ;
2007-04-28 23:47:13 +04:00
for ( i = 0 ; i < keys . num ; i + + ) {
printf ( " key: " ) ;
for ( j = 0 ; j < keys . keys [ i ] . dsize ; j + + ) {
printf ( " %02x " , keys . keys [ i ] . dptr [ j ] ) ;
}
printf ( " lmaster:%d rsn:%llu dmaster:%d laccessor:%d lacount:%d " , keys . lmasters [ i ] , keys . headers [ i ] . rsn , keys . headers [ i ] . dmaster , keys . headers [ i ] . laccessor , keys . headers [ i ] . lacount ) ;
printf ( " data: " ) ;
for ( j = 0 ; j < keys . data [ i ] . dsize ; j + + ) {
printf ( " %02x " , keys . data [ i ] . dptr [ j ] ) ;
}
printf ( " \n " ) ;
}
talloc_free ( mem_ctx ) ;
return 0 ;
}
2007-04-29 18:58:27 +04:00
/*
copy a db from one node to another
*/
static int control_cpdb ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t fromvnn , tovnn , dbid ;
int ret ;
TALLOC_CTX * mem_ctx ;
if ( argc < 3 ) {
usage ( ) ;
}
fromvnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
tovnn = strtoul ( argv [ 1 ] , NULL , 0 ) ;
dbid = strtoul ( argv [ 2 ] , NULL , 0 ) ;
mem_ctx = talloc_new ( ctdb ) ;
2007-05-02 04:20:34 +04:00
ret = ctdb_ctrl_copydb ( ctdb , fromvnn , tovnn , dbid , CTDB_LMASTER_ANY , mem_ctx ) ;
2007-04-29 18:58:27 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to copy db from node %u to node %u \n " , fromvnn , tovnn ) ;
return ret ;
}
talloc_free ( mem_ctx ) ;
return 0 ;
}
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 )
{
uint32_t vnn ;
int i , ret ;
2007-05-02 04:20:34 +04:00
struct ctdb_dbid_map dbmap ;
2007-04-28 14:00:50 +04:00
if ( argc < 1 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-05-02 04:20:34 +04:00
ret = ctdb_ctrl_getdbmap ( ctdb , vnn , ctdb , & dbmap ) ;
2007-04-28 14:00:50 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to get dbids from node %u \n " , vnn ) ;
return ret ;
}
2007-05-02 04:20: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-02 04:20:34 +04:00
ctdb_ctrl_getdbpath ( ctdb , dbmap . dbids [ i ] , ctdb , & path ) ;
printf ( " dbid:0x%08x path:%s \n " , dbmap . dbids [ i ] , 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
/*
display a list nodes known to a remote ctdb
*/
static int control_getnodemap ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn ;
int i , ret ;
struct ctdb_node_map * nodemap ;
if ( argc < 1 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
nodemap = talloc_zero ( ctdb , struct ctdb_node_map ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_getnodemap ( ctdb , vnn , nodemap , nodemap ) ;
2007-04-28 14:40:26 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to get nodemap from node %u \n " , vnn ) ;
talloc_free ( nodemap ) ;
return ret ;
}
printf ( " Number of nodes:%d \n " , nodemap - > num ) ;
for ( i = 0 ; i < nodemap - > num ; i + + ) {
2007-05-02 05:06:58 +04:00
printf ( " vnn:%d %s%s \n " , nodemap - > nodes [ i ] . vnn ,
2007-04-28 17:11:23 +04:00
nodemap - > nodes [ i ] . flags & NODE_FLAGS_CONNECTED ?
2007-05-02 05:06:58 +04:00
" CONNECTED " : " UNAVAILABLE " ,
nodemap - > nodes [ i ] . vnn = = vnn ? " (THIS NODE) " : " " ) ;
2007-04-28 14:40:26 +04:00
}
talloc_free ( nodemap ) ;
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
set remote ctdb vnn map
*/
2007-04-27 16:08:12 +04:00
static int control_setvnnmap ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn ;
struct ctdb_vnn_map * vnnmap ;
int i , ret ;
if ( argc < 3 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
vnnmap = talloc_zero ( ctdb , struct ctdb_vnn_map ) ;
vnnmap - > generation = strtoul ( argv [ 1 ] , NULL , 0 ) ;
vnnmap - > size = strtoul ( argv [ 2 ] , NULL , 0 ) ;
vnnmap - > map = talloc_array ( vnnmap , uint32_t , vnnmap - > size ) ;
for ( i = 0 ; i < vnnmap - > size ; i + + ) {
vnnmap - > map [ i ] = strtoul ( argv [ 3 + i ] , NULL , 0 ) ;
}
2007-05-02 04:20:34 +04:00
ret = ctdb_ctrl_setvnnmap ( ctdb , vnn , ctdb , vnnmap ) ;
2007-04-27 16:08:12 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to set vnnmap for node %u \n " , vnn ) ;
return ret ;
}
return 0 ;
}
2007-04-29 12:34:11 +04:00
/*
set the dmaster for all records in a database
*/
static int control_setdmaster ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn , dbid , dmaster ;
int ret ;
2007-04-29 12:48:46 +04:00
if ( argc < 3 ) {
2007-04-29 12:34:11 +04:00
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
dbid = strtoul ( argv [ 1 ] , NULL , 0 ) ;
dmaster = strtoul ( argv [ 2 ] , NULL , 0 ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_setdmaster ( ctdb , vnn , ctdb , dbid , dmaster ) ;
2007-04-29 12:34:11 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to set dmaster for node %u db:0x%08x \n " , vnn , dbid ) ;
return ret ;
}
return 0 ;
}
2007-04-29 12:48:46 +04:00
/*
clears a database
*/
static int control_cleardb ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
uint32_t vnn , dbid ;
int ret ;
if ( argc < 2 ) {
usage ( ) ;
}
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
dbid = strtoul ( argv [ 1 ] , NULL , 0 ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_cleardb ( ctdb , vnn , ctdb , dbid ) ;
2007-04-29 12:48:46 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to clear db for node %u db:0x%08x \n " , vnn , dbid ) ;
return ret ;
}
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
ping all node
*/
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-04-28 19:42:40 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , ctdb , & num_nodes ) ;
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
/*
display debug level on all node
*/
static int control_debuglevel ( 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 ;
nodes = ctdb_get_connected_nodes ( ctdb , ctdb , & num_nodes ) ;
CTDB_NO_MEMORY ( ctdb , nodes ) ;
2007-04-27 17:14:36 +04:00
2007-04-28 19:42:40 +04:00
for ( i = 0 ; i < num_nodes ; i + + ) {
2007-04-27 17:14:36 +04:00
uint32_t level ;
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 ;
}
/*
set debug level on a node
*/
static int control_debug ( struct ctdb_context * ctdb , int argc , const char * * argv )
{
int ret ;
2007-04-28 19:13:30 +04:00
uint32_t vnn , 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
if ( argc < 2 ) {
usage ( ) ;
}
level = strtoul ( argv [ 1 ] , NULL , 0 ) ;
2007-04-28 19:42:40 +04:00
if ( strcmp ( argv [ 0 ] , " all " ) ! = 0 ) {
vnn = strtoul ( argv [ 0 ] , NULL , 0 ) ;
2007-04-29 18:58:27 +04:00
ret = ctdb_ctrl_set_debuglevel ( ctdb , vnn , level ) ;
2007-04-28 19:42:40 +04:00
if ( ret ! = 0 ) {
printf ( " Unable to set debug level on node %u \n " , vnn ) ;
2007-04-28 19:13:30 +04:00
}
2007-04-28 19:42:40 +04:00
2007-04-28 19:13:30 +04:00
return 0 ;
}
2007-04-28 19:42:40 +04:00
nodes = ctdb_get_connected_nodes ( ctdb , ctdb , & num_nodes ) ;
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
/*
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 ;
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-04-26 16:27:49 +04:00
/*
main program
*/
int main ( int argc , const char * argv [ ] )
{
struct ctdb_context * ctdb ;
struct poptOption popt_options [ ] = {
POPT_AUTOHELP
POPT_CTDB_CMDLINE
POPT_TABLEEND
} ;
int opt ;
const char * * extra_argv ;
int extra_argc = 0 ;
int ret ;
poptContext pc ;
struct event_context * ev ;
const char * control ;
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 ( ) ;
}
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 ) ;
}
if ( strcmp ( control , " process-exists " ) = = 0 ) {
ret = control_process_exists ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-26 16:51:41 +04:00
} else if ( strcmp ( control , " status " ) = = 0 ) {
ret = control_status ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-28 21:13:36 +04:00
} else if ( strcmp ( control , " statusreset " ) = = 0 ) {
ret = control_status_reset ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-27 14:56:10 +04:00
} else if ( strcmp ( control , " getvnnmap " ) = = 0 ) {
ret = control_getvnnmap ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-28 14:00:50 +04:00
} else if ( strcmp ( control , " getdbmap " ) = = 0 ) {
ret = control_getdbmap ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-28 14:40:26 +04:00
} else if ( strcmp ( control , " getnodemap " ) = = 0 ) {
ret = control_getnodemap ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-29 18:58:27 +04:00
} else if ( strcmp ( control , " catdb " ) = = 0 ) {
ret = control_catdb ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
} else if ( strcmp ( control , " cpdb " ) = = 0 ) {
ret = control_cpdb ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-27 16:08:12 +04:00
} else if ( strcmp ( control , " setvnnmap " ) = = 0 ) {
ret = control_setvnnmap ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-29 12:34:11 +04:00
} else if ( strcmp ( control , " setdmaster " ) = = 0 ) {
ret = control_setdmaster ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-29 12:48:46 +04:00
} else if ( strcmp ( control , " cleardb " ) = = 0 ) {
ret = control_cleardb ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-29 16:51:56 +04:00
} else if ( strcmp ( control , " getrecmode " ) = = 0 ) {
ret = control_getrecmode ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
} else if ( strcmp ( control , " setrecmode " ) = = 0 ) {
ret = control_setrecmode ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-26 21:27:07 +04:00
} else if ( strcmp ( control , " ping " ) = = 0 ) {
ret = control_ping ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-27 17:14:36 +04:00
} else if ( strcmp ( control , " debug " ) = = 0 ) {
ret = control_debug ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
} else if ( strcmp ( control , " debuglevel " ) = = 0 ) {
ret = control_debuglevel ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-05-02 04:20:34 +04:00
} else if ( strcmp ( control , " recover " ) = = 0 ) {
ret = control_recover ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-30 17:54:06 +04:00
} else if ( strcmp ( control , " attach " ) = = 0 ) {
ret = control_attach ( ctdb , extra_argc - 1 , extra_argv + 1 ) ;
2007-04-26 16:27:49 +04:00
} else {
printf ( " Unknown control '%s' \n " , control ) ;
exit ( 1 ) ;
}
return ret ;
}