1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240
|
/*
ctdb control tool
Copyright (C) Andrew Tridgell 2007
Copyright (C) Ronnie Sahlberg 2007
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 3 of the License, or
(at your option) any later version.
This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/events/events.h"
#include "system/time.h"
#include "system/filesys.h"
#include "system/network.h"
#include "system/locale.h"
#include "popt.h"
#include "cmdline.h"
#include "../include/ctdb.h"
#include "../include/ctdb_private.h"
#include "../common/rb_tree.h"
#include "db_wrap.h"
#define ERR_TIMEOUT 20 /* timed out trying to reach node */
#define ERR_NONODE 21 /* node does not exist */
#define ERR_DISNODE 22 /* node is disconnected */
static void usage(void);
static struct {
int timelimit;
uint32_t pnn;
int machinereadable;
int maxruntime;
} options;
#define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
#ifdef CTDB_VERS
static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
{
#define STR(x) #x
#define XSTR(x) STR(x)
printf("CTDB version: %s\n", XSTR(CTDB_VERS));
return 0;
}
#endif
/*
verify that a node exists and is reachable
*/
static void verify_node(struct ctdb_context *ctdb)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
if (options.pnn == CTDB_CURRENT_NODE) {
return;
}
if (options.pnn == CTDB_BROADCAST_ALL) {
return;
}
/* verify the node exists */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
if (options.pnn >= nodemap->num) {
DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
exit(ERR_NONODE);
}
if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DELETED) {
DEBUG(DEBUG_ERR, ("Node %u is DELETED\n", options.pnn));
exit(ERR_DISNODE);
}
if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
exit(ERR_DISNODE);
}
/* verify we can access the node */
ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
if (ret == -1) {
DEBUG(DEBUG_ERR,("Can not ban node. Node is not operational.\n"));
exit(10);
}
}
/*
check if a database exists
*/
static int db_exists(struct ctdb_context *ctdb, const char *db_name)
{
int i, ret;
struct ctdb_dbid_map *dbmap=NULL;
ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
return -1;
}
for(i=0;i<dbmap->num;i++){
const char *name;
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
if (!strcmp(name, db_name)) {
return 0;
}
}
return -1;
}
/*
see if a process exists
*/
static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t pnn, pid;
int ret;
if (argc < 1) {
usage();
}
if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
return -1;
}
ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
if (ret == 0) {
printf("%u:%u exists\n", pnn, pid);
} else {
printf("%u:%u does not exist\n", pnn, pid);
}
return ret;
}
/*
display statistics structure
*/
static void show_statistics(struct ctdb_statistics *s)
{
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
int i;
const char *prefix=NULL;
int preflen=0;
const struct {
const char *name;
uint32_t offset;
} fields[] = {
#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_control),
STATISTICS_FIELD(node.reply_control),
STATISTICS_FIELD(client.req_call),
STATISTICS_FIELD(client.req_message),
STATISTICS_FIELD(client.req_control),
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(childwrite_calls),
STATISTICS_FIELD(pending_childwrite_calls),
STATISTICS_FIELD(memory_used),
STATISTICS_FIELD(max_hop_count),
};
printf("CTDB version %u\n", CTDB_VERSION);
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_reclock_ctdbd", s->reclock.ctdbd);
printf(" %-30s %.6f sec\n", "max_reclock_recd", s->reclock.recd);
printf(" %-30s %.6f sec\n", "max_call_latency", s->max_call_latency);
printf(" %-30s %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
printf(" %-30s %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
talloc_free(tmp_ctx);
}
/*
display remote ctdb statistics combined from all nodes
*/
static int control_statistics_all(struct ctdb_context *ctdb)
{
int ret, i;
struct ctdb_statistics statistics;
uint32_t *nodes;
uint32_t num_nodes;
nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
CTDB_NO_MEMORY(ctdb, nodes);
ZERO_STRUCT(statistics);
for (i=0;i<num_nodes;i++) {
struct ctdb_statistics s1;
int j;
uint32_t *v1 = (uint32_t *)&s1;
uint32_t *v2 = (uint32_t *)&statistics;
uint32_t num_ints =
offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
return ret;
}
for (j=0;j<num_ints;j++) {
v2[j] += v1[j];
}
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);
}
talloc_free(nodes);
printf("Gathered statistics for %u nodes\n", num_nodes);
show_statistics(&statistics);
return 0;
}
/*
display remote ctdb statistics
*/
static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_statistics statistics;
if (options.pnn == CTDB_BROADCAST_ALL) {
return control_statistics_all(ctdb);
}
ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
return ret;
}
show_statistics(&statistics);
return 0;
}
/*
reset remote ctdb statistics
*/
static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
ret = ctdb_statistics_reset(ctdb, options.pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
display uptime of remote node
*/
static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_uptime *uptime = NULL;
int tmp, days, hours, minutes, seconds;
ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
return ret;
}
if (options.machinereadable){
printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
printf(":%u:%u:%u:%lf\n",
(unsigned int)uptime->current_time.tv_sec,
(unsigned int)uptime->ctdbd_start_time.tv_sec,
(unsigned int)uptime->last_recovery_finished.tv_sec,
timeval_delta(&uptime->last_recovery_finished,
&uptime->last_recovery_started)
);
return 0;
}
printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
seconds = tmp%60;
tmp /= 60;
minutes = tmp%60;
tmp /= 60;
hours = tmp%24;
tmp /= 24;
days = tmp;
printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
seconds = tmp%60;
tmp /= 60;
minutes = tmp%60;
tmp /= 60;
hours = tmp%24;
tmp /= 24;
days = tmp;
printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
printf("Duration of last recovery/failover: %lf seconds\n",
timeval_delta(&uptime->last_recovery_finished,
&uptime->last_recovery_started));
return 0;
}
/*
show the PNN of the current node
*/
static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
{
int mypnn;
mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
if (mypnn == -1) {
DEBUG(DEBUG_ERR, ("Unable to get pnn from local node."));
return -1;
}
printf("PNN:%d\n", mypnn);
return 0;
}
struct pnn_node {
struct pnn_node *next;
const char *addr;
int pnn;
};
static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
{
const char *nodes_list;
int nlines;
char **lines;
int i, pnn;
struct pnn_node *pnn_nodes = NULL;
struct pnn_node *pnn_node;
struct pnn_node *tmp_node;
/* read the nodes file */
nodes_list = getenv("CTDB_NODES");
if (nodes_list == NULL) {
nodes_list = "/etc/ctdb/nodes";
}
lines = file_lines_load(nodes_list, &nlines, mem_ctx);
if (lines == NULL) {
return NULL;
}
while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
nlines--;
}
for (i=0, pnn=0; i<nlines; i++) {
char *node;
node = lines[i];
/* strip leading spaces */
while((*node == ' ') || (*node == '\t')) {
node++;
}
if (*node == '#') {
pnn++;
continue;
}
if (strcmp(node, "") == 0) {
continue;
}
pnn_node = talloc(mem_ctx, struct pnn_node);
pnn_node->pnn = pnn++;
pnn_node->addr = talloc_strdup(pnn_node, node);
pnn_node->next = pnn_nodes;
pnn_nodes = pnn_node;
}
/* swap them around so we return them in incrementing order */
pnn_node = pnn_nodes;
pnn_nodes = NULL;
while (pnn_node) {
tmp_node = pnn_node;
pnn_node = pnn_node->next;
tmp_node->next = pnn_nodes;
pnn_nodes = tmp_node;
}
return pnn_nodes;
}
/*
show the PNN of the current node
discover the pnn by loading the nodes file and try to bind to all
addresses one at a time until the ip address is found.
*/
static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
{
TALLOC_CTX *mem_ctx = talloc_new(NULL);
struct pnn_node *pnn_nodes;
struct pnn_node *pnn_node;
pnn_nodes = read_nodes_file(mem_ctx);
if (pnn_nodes == NULL) {
DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
talloc_free(mem_ctx);
return -1;
}
for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
ctdb_sock_addr addr;
if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
talloc_free(mem_ctx);
return -1;
}
if (ctdb_sys_have_ip(&addr)) {
printf("PNN:%d\n", pnn_node->pnn);
talloc_free(mem_ctx);
return 0;
}
}
printf("Failed to detect which PNN this node is\n");
talloc_free(mem_ctx);
return -1;
}
/*
display remote ctdb status
*/
static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
struct ctdb_vnn_map *vnnmap=NULL;
struct ctdb_node_map *nodemap=NULL;
uint32_t recmode, recmaster;
int mypnn;
mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
if (mypnn == -1) {
return -1;
}
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
return ret;
}
if(options.machinereadable){
printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped:\n");
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
continue;
}
printf(":%d:%s:%d:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
ctdb_addr_to_str(&nodemap->nodes[i].addr),
!!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
!!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
!!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
!!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
!!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
}
return 0;
}
printf("Number of nodes:%d\n", nodemap->num);
for(i=0;i<nodemap->num;i++){
static const struct {
uint32_t flag;
const char *name;
} flag_names[] = {
{ NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
{ NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
{ NODE_FLAGS_BANNED, "BANNED" },
{ NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
{ NODE_FLAGS_DELETED, "DELETED" },
{ NODE_FLAGS_STOPPED, "STOPPED" },
};
char *flags_str = NULL;
int j;
if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
continue;
}
for (j=0;j<ARRAY_SIZE(flag_names);j++) {
if (nodemap->nodes[i].flags & flag_names[j].flag) {
if (flags_str == NULL) {
flags_str = talloc_strdup(ctdb, flag_names[j].name);
} else {
flags_str = talloc_asprintf_append(flags_str, "|%s",
flag_names[j].name);
}
CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
}
}
if (flags_str == NULL) {
flags_str = talloc_strdup(ctdb, "OK");
CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
}
printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
ctdb_addr_to_str(&nodemap->nodes[i].addr),
flags_str,
nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
talloc_free(flags_str);
}
ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
return ret;
}
if (vnnmap->generation == INVALID_GENERATION) {
printf("Generation:INVALID\n");
} else {
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]);
}
ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
return ret;
}
printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
return ret;
}
printf("Recovery master:%d\n",recmaster);
return 0;
}
struct natgw_node {
struct natgw_node *next;
const char *addr;
};
/*
display the list of nodes belonging to this natgw configuration
*/
static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
const char *natgw_list;
int nlines;
char **lines;
struct natgw_node *natgw_nodes = NULL;
struct natgw_node *natgw_node;
struct ctdb_node_map *nodemap=NULL;
/* read the natgw nodes file into a linked list */
natgw_list = getenv("NATGW_NODES");
if (natgw_list == NULL) {
natgw_list = "/etc/ctdb/natgw_nodes";
}
lines = file_lines_load(natgw_list, &nlines, ctdb);
if (lines == NULL) {
ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
return -1;
}
while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
nlines--;
}
for (i=0;i<nlines;i++) {
char *node;
node = lines[i];
/* strip leading spaces */
while((*node == ' ') || (*node == '\t')) {
node++;
}
if (*node == '#') {
continue;
}
if (strcmp(node, "") == 0) {
continue;
}
natgw_node = talloc(ctdb, struct natgw_node);
natgw_node->addr = talloc_strdup(natgw_node, node);
CTDB_NO_MEMORY(ctdb, natgw_node->addr);
natgw_node->next = natgw_nodes;
natgw_nodes = natgw_node;
}
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
return ret;
}
i=0;
while(i<nodemap->num) {
for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
break;
}
}
/* this node was not in the natgw so we just remove it from
* the list
*/
if ((natgw_node == NULL)
|| (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
int j;
for (j=i+1; j<nodemap->num; j++) {
nodemap->nodes[j-1] = nodemap->nodes[j];
}
nodemap->num--;
continue;
}
i++;
}
/* pick a node to be natgwmaster
* we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
*/
for(i=0;i<nodemap->num;i++){
if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
break;
}
}
/* we couldnt find any healthy node, try unhealthy ones */
if (i == nodemap->num) {
for(i=0;i<nodemap->num;i++){
if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
break;
}
}
}
/* unless all nodes are STOPPED, when we pick one anyway */
if (i == nodemap->num) {
for(i=0;i<nodemap->num;i++){
if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
break;
}
}
/* or if we still can not find any */
if (i == nodemap->num) {
printf("-1 0.0.0.0\n");
}
}
/* print the pruned list of nodes belonging to this natgw list */
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
continue;
}
printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
ctdb_addr_to_str(&nodemap->nodes[i].addr),
!!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
!!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
!!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
!!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
!!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
}
return 0;
}
/*
display the status of the scripts for monitoring (or other events)
*/
static int control_one_scriptstatus(struct ctdb_context *ctdb,
enum ctdb_eventscript_call type)
{
struct ctdb_scripts_wire *script_status;
int ret, i;
ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
return ret;
}
if (script_status == NULL) {
if (!options.machinereadable) {
printf("%s cycle never run\n",
ctdb_eventscript_call_names[type]);
}
return 0;
}
if (!options.machinereadable) {
printf("%d scripts were executed last %s cycle\n",
script_status->num_scripts,
ctdb_eventscript_call_names[type]);
}
for (i=0; i<script_status->num_scripts; i++) {
const char *status = NULL;
switch (script_status->scripts[i].status) {
case -ETIME:
status = "TIMEDOUT";
break;
case -ENOEXEC:
status = "DISABLED";
break;
case 0:
status = "OK";
break;
default:
if (script_status->scripts[i].status > 0)
status = "ERROR";
break;
}
if (options.machinereadable) {
printf("%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
ctdb_eventscript_call_names[type],
script_status->scripts[i].name,
script_status->scripts[i].status,
status,
(long)script_status->scripts[i].start.tv_sec,
(long)script_status->scripts[i].start.tv_usec,
(long)script_status->scripts[i].finished.tv_sec,
(long)script_status->scripts[i].finished.tv_usec,
script_status->scripts[i].output);
continue;
}
if (status)
printf("%-20s Status:%s ",
script_status->scripts[i].name, status);
else
/* Some other error, eg from stat. */
printf("%-20s Status:CANNOT RUN (%s)",
script_status->scripts[i].name,
strerror(-script_status->scripts[i].status));
if (script_status->scripts[i].status >= 0) {
printf("Duration:%.3lf ",
timeval_delta(&script_status->scripts[i].finished,
&script_status->scripts[i].start));
}
if (script_status->scripts[i].status != -ENOEXEC) {
printf("%s",
ctime(&script_status->scripts[i].start.tv_sec));
if (script_status->scripts[i].status != 0) {
printf(" OUTPUT:%s\n",
script_status->scripts[i].output);
}
} else {
printf("\n");
}
}
return 0;
}
static int control_scriptstatus(struct ctdb_context *ctdb,
int argc, const char **argv)
{
int ret;
enum ctdb_eventscript_call type, min, max;
const char *arg;
if (argc > 1) {
DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
return -1;
}
if (argc == 0)
arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
else
arg = argv[0];
for (type = 0; type < CTDB_EVENT_MAX; type++) {
if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
min = type;
max = type+1;
break;
}
}
if (type == CTDB_EVENT_MAX) {
if (strcmp(arg, "all") == 0) {
min = 0;
max = CTDB_EVENT_MAX;
} else {
DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
return -1;
}
}
if (options.machinereadable) {
printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
}
for (type = min; type < max; type++) {
ret = control_one_scriptstatus(ctdb, type);
if (ret != 0) {
return ret;
}
}
return 0;
}
/*
enable an eventscript
*/
static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
if (argc < 1) {
usage();
}
ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
return ret;
}
return 0;
}
/*
disable an eventscript
*/
static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
if (argc < 1) {
usage();
}
ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
return ret;
}
return 0;
}
/*
display the pnn of the recovery master
*/
static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t recmaster;
ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
return ret;
}
printf("%d\n",recmaster);
return 0;
}
/*
get a list of all tickles for this pnn
*/
static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
{
struct ctdb_control_tcp_tickle_list *list;
ctdb_sock_addr addr;
int i, ret;
if (argc < 1) {
usage();
}
if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
return -1;
}
ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
if (ret == -1) {
DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
return -1;
}
printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
printf("Num tickles:%u\n", list->tickles.num);
for (i=0;i<list->tickles.num;i++) {
printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
}
talloc_free(list);
return 0;
}
static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
{
struct ctdb_all_public_ips *ips;
struct ctdb_public_ip ip;
int i, ret;
uint32_t *nodes;
uint32_t disable_time;
TDB_DATA data;
struct ctdb_node_map *nodemap=NULL;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
disable_time = 30;
data.dptr = (uint8_t*)&disable_time;
data.dsize = sizeof(disable_time);
ret = ctdb_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
return -1;
}
/* read the public ip list from the node */
ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
talloc_free(tmp_ctx);
return -1;
}
for (i=0;i<ips->num;i++) {
if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
break;
}
}
if (i==ips->num) {
DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
pnn, ctdb_addr_to_str(addr)));
talloc_free(tmp_ctx);
return -1;
}
ip.pnn = pnn;
ip.addr = *addr;
data.dptr = (uint8_t *)&ip;
data.dsize = sizeof(ip);
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
nodes, 0,
TIMELIMIT(),
false, data,
NULL, NULL,
NULL);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
talloc_free(tmp_ctx);
return -1;
}
ret = ctdb_ctrl_takeover_ip(ctdb, TIMELIMIT(), pnn, &ip);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
talloc_free(tmp_ctx);
return -1;
}
talloc_free(tmp_ctx);
return 0;
}
/*
move/failover an ip address to a specific node
*/
static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t pnn;
ctdb_sock_addr addr;
if (argc < 2) {
usage();
return -1;
}
if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
return -1;
}
if (sscanf(argv[1], "%u", &pnn) != 1) {
DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
return -1;
}
if (move_ip(ctdb, &addr, pnn) != 0) {
DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", pnn));
return -1;
}
return 0;
}
void getips_store_callback(void *param, void *data)
{
struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
struct ctdb_all_public_ips *ips = param;
int i;
i = ips->num++;
ips->ips[i].pnn = node_ip->pnn;
ips->ips[i].addr = node_ip->addr;
}
void getips_count_callback(void *param, void *data)
{
uint32_t *count = param;
(*count)++;
}
#define IP_KEYLEN 4
static uint32_t *ip_key(ctdb_sock_addr *ip)
{
static uint32_t key[IP_KEYLEN];
bzero(key, sizeof(key));
switch (ip->sa.sa_family) {
case AF_INET:
key[0] = ip->ip.sin_addr.s_addr;
break;
case AF_INET6:
key[0] = ip->ip6.sin6_addr.s6_addr32[3];
key[1] = ip->ip6.sin6_addr.s6_addr32[2];
key[2] = ip->ip6.sin6_addr.s6_addr32[1];
key[3] = ip->ip6.sin6_addr.s6_addr32[0];
break;
default:
DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
return key;
}
return key;
}
static void *add_ip_callback(void *parm, void *data)
{
return parm;
}
static int
control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
{
struct ctdb_all_public_ips *tmp_ips;
struct ctdb_node_map *nodemap=NULL;
trbt_tree_t *ip_tree;
int i, j, len, ret;
uint32_t count;
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
return ret;
}
ip_tree = trbt_create(tmp_ctx, 0);
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
continue;
}
if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
continue;
}
/* read the public ip list from this node */
ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
return -1;
}
for (j=0; j<tmp_ips->num;j++) {
struct ctdb_public_ip *node_ip;
node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
node_ip->pnn = tmp_ips->ips[j].pnn;
node_ip->addr = tmp_ips->ips[j].addr;
trbt_insertarray32_callback(ip_tree,
IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
add_ip_callback,
node_ip);
}
talloc_free(tmp_ips);
}
/* traverse */
count = 0;
trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
len = offsetof(struct ctdb_all_public_ips, ips) +
count*sizeof(struct ctdb_public_ip);
tmp_ips = talloc_zero_size(tmp_ctx, len);
trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
*ips = tmp_ips;
return 0;
}
/*
* scans all other nodes and returns a pnn for another node that can host this
* ip address or -1
*/
static int
find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
{
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
struct ctdb_all_public_ips *ips;
struct ctdb_node_map *nodemap=NULL;
int i, j, ret;
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (nodemap->nodes[i].pnn == options.pnn) {
continue;
}
/* read the public ip list from this node */
ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
return -1;
}
for (j=0;j<ips->num;j++) {
if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
talloc_free(tmp_ctx);
return nodemap->nodes[i].pnn;
}
}
talloc_free(ips);
}
talloc_free(tmp_ctx);
return -1;
}
/*
add a public ip address to a node
*/
static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
int len;
uint32_t pnn;
unsigned mask;
ctdb_sock_addr addr;
struct ctdb_control_ip_iface *pub;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
struct ctdb_all_public_ips *ips;
if (argc != 2) {
talloc_free(tmp_ctx);
usage();
}
if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
talloc_free(tmp_ctx);
return -1;
}
ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
talloc_free(tmp_ctx);
return ret;
}
/* check if some other node is already serving this ip, if not,
* we will claim it
*/
for (i=0;i<ips->num;i++) {
if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
break;
}
}
len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
pub = talloc_size(tmp_ctx, len);
CTDB_NO_MEMORY(ctdb, pub);
pub->addr = addr;
pub->mask = mask;
pub->len = strlen(argv[1])+1;
memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
if (i == ips->num) {
/* no one has this ip so we claim it */
pnn = options.pnn;
} else {
pnn = ips->ips[i].pnn;
}
if (move_ip(ctdb, &addr, pnn) != 0) {
DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", pnn));
return -1;
}
talloc_free(tmp_ctx);
return 0;
}
static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
{
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
struct ctdb_node_map *nodemap=NULL;
struct ctdb_all_public_ips *ips;
int ret, i, j;
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
return ret;
}
/* remove it from the nodes that are not hosting the ip currently */
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
continue;
}
for (j=0;j<ips->num;j++) {
if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
break;
}
}
if (j==ips->num) {
continue;
}
if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
continue;
}
options.pnn = nodemap->nodes[i].pnn;
control_delip(ctdb, argc, argv);
}
/* remove it from every node (also the one hosting it) */
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
continue;
}
for (j=0;j<ips->num;j++) {
if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
break;
}
}
if (j==ips->num) {
continue;
}
options.pnn = nodemap->nodes[i].pnn;
control_delip(ctdb, argc, argv);
}
talloc_free(tmp_ctx);
return 0;
}
/*
delete a public ip address from a node
*/
static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
ctdb_sock_addr addr;
struct ctdb_control_ip_iface pub;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
struct ctdb_all_public_ips *ips;
if (argc != 1) {
talloc_free(tmp_ctx);
usage();
}
if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
return -1;
}
if (options.pnn == CTDB_BROADCAST_ALL) {
return control_delip_all(ctdb, argc, argv, &addr);
}
pub.addr = addr;
pub.mask = 0;
pub.len = 0;
ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
talloc_free(tmp_ctx);
return ret;
}
for (i=0;i<ips->num;i++) {
if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
break;
}
}
if (i==ips->num) {
DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
ctdb_addr_to_str(&addr)));
talloc_free(tmp_ctx);
return -1;
}
if (ips->ips[i].pnn == options.pnn) {
ret = find_other_host_for_public_ip(ctdb, &addr);
if (ret != -1) {
if (move_ip(ctdb, &addr, ret) != 0) {
DEBUG(DEBUG_ERR,("Failed to move ip to node %d\n", ret));
return -1;
}
}
}
ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
talloc_free(tmp_ctx);
return 0;
}
/*
kill a tcp connection
*/
static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_control_killtcp killtcp;
if (argc < 2) {
usage();
}
if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
return -1;
}
if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
return -1;
}
ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
send a gratious arp
*/
static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
ctdb_sock_addr addr;
if (argc < 2) {
usage();
}
if (!parse_ip(argv[0], NULL, 0, &addr)) {
DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
return -1;
}
ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
register a server id
*/
static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_server_id server_id;
if (argc < 3) {
usage();
}
server_id.pnn = strtoul(argv[0], NULL, 0);
server_id.type = strtoul(argv[1], NULL, 0);
server_id.server_id = strtoul(argv[2], NULL, 0);
ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
return ret;
}
return -1;
}
/*
unregister a server id
*/
static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_server_id server_id;
if (argc < 3) {
usage();
}
server_id.pnn = strtoul(argv[0], NULL, 0);
server_id.type = strtoul(argv[1], NULL, 0);
server_id.server_id = strtoul(argv[2], NULL, 0);
ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
return ret;
}
return -1;
}
/*
check if a server id exists
*/
static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t status;
int ret;
struct ctdb_server_id server_id;
if (argc < 3) {
usage();
}
server_id.pnn = strtoul(argv[0], NULL, 0);
server_id.type = strtoul(argv[1], NULL, 0);
server_id.server_id = strtoul(argv[2], NULL, 0);
ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
return ret;
}
if (status) {
printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
} else {
printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
}
return 0;
}
/*
get a list of all server ids that are registered on a node
*/
static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
struct ctdb_server_id_list *server_ids;
ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
return ret;
}
for (i=0; i<server_ids->num; i++) {
printf("Server id %d:%d:%d\n",
server_ids->server_ids[i].pnn,
server_ids->server_ids[i].type,
server_ids->server_ids[i].server_id);
}
return -1;
}
/*
send a tcp tickle ack
*/
static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
ctdb_sock_addr src, dst;
if (argc < 2) {
usage();
}
if (!parse_ip_port(argv[0], &src)) {
DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
return -1;
}
if (!parse_ip_port(argv[1], &dst)) {
DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
return -1;
}
ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
if (ret==0) {
return 0;
}
DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
return -1;
}
/*
display public ip status
*/
static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
struct ctdb_all_public_ips *ips;
if (options.pnn == CTDB_BROADCAST_ALL) {
/* read the list of public ips from all nodes */
ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
} else {
/* read the public ip list from this node */
ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
}
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
if (options.machinereadable){
printf(":Public IP:Node:\n");
} else {
if (options.pnn == CTDB_BROADCAST_ALL) {
printf("Public IPs on ALL nodes\n");
} else {
printf("Public IPs on node %u\n", options.pnn);
}
}
for (i=1;i<=ips->num;i++) {
if (options.machinereadable){
printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
} else {
printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
}
}
talloc_free(tmp_ctx);
return 0;
}
/*
display pid of a ctdb daemon
*/
static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t pid;
int ret;
ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
return ret;
}
printf("Pid:%d\n", pid);
return 0;
}
/*
handler for receiving the response to ipreallocate
*/
static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
TDB_DATA data, void *private_data)
{
exit(0);
}
static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
{
struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
event_add_timed(ctdb->ev, ctdb,
timeval_current_ofs(1, 0),
ctdb_every_second, ctdb);
}
/*
ask the recovery daemon on the recovery master to perform a ip reallocation
*/
static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
TDB_DATA data;
struct takeover_run_reply rd;
uint32_t recmaster;
struct ctdb_node_map *nodemap=NULL;
int retries=0;
struct timeval tv = timeval_current();
/* we need some events to trigger so we can timeout and restart
the loop
*/
event_add_timed(ctdb->ev, ctdb,
timeval_current_ofs(1, 0),
ctdb_every_second, ctdb);
rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
if (rd.pnn == -1) {
DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
return -1;
}
rd.srvid = getpid();
/* register a message port for receiveing the reply so that we
can receive the reply
*/
ctdb_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
data.dptr = (uint8_t *)&rd;
data.dsize = sizeof(rd);
again:
if (retries>5) {
DEBUG(DEBUG_ERR,("Failed waiting for cluster convergense\n"));
exit(10);
}
/* check that there are valid nodes available */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
for (i=0; i<nodemap->num;i++) {
if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
break;
}
}
if (i==nodemap->num) {
DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
return 0;
}
ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
return ret;
}
/* verify the node exists */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
/* check tha there are nodes available that can act as a recmaster */
for (i=0; i<nodemap->num; i++) {
if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
continue;
}
break;
}
if (i == nodemap->num) {
DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
return 0;
}
/* verify the recovery master is not STOPPED, nor BANNED */
if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
retries++;
sleep(1);
goto again;
}
/* verify the recovery master is not STOPPED, nor BANNED */
if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
retries++;
sleep(1);
goto again;
}
ret = ctdb_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
return -1;
}
tv = timeval_current();
/* this loop will terminate when we have received the reply */
while (timeval_elapsed(&tv) < 3.0) {
event_loop_once(ctdb->ev);
}
DEBUG(DEBUG_INFO,("Timed out waiting for recmaster ipreallocate. Trying again\n"));
retries++;
sleep(1);
goto again;
return 0;
}
/*
disable a remote node
*/
static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
do {
ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
return ret;
}
sleep(1);
/* read the nodemap and verify the change took effect */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
} while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
ret = control_ipreallocate(ctdb, argc, argv);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
enable a disabled remote node
*/
static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
do {
ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
return ret;
}
sleep(1);
/* read the nodemap and verify the change took effect */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
} while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
ret = control_ipreallocate(ctdb, argc, argv);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
stop a remote node
*/
static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
do {
ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
}
sleep(1);
/* read the nodemap and verify the change took effect */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
} while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
ret = control_ipreallocate(ctdb, argc, argv);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
restart a stopped remote node
*/
static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
do {
ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
return ret;
}
sleep(1);
/* read the nodemap and verify the change took effect */
if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
exit(10);
}
} while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
ret = control_ipreallocate(ctdb, argc, argv);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
return ret;
}
return 0;
}
static uint32_t get_generation(struct ctdb_context *ctdb)
{
struct ctdb_vnn_map *vnnmap=NULL;
int ret;
/* wait until the recmaster is not in recovery mode */
while (1) {
uint32_t recmode, recmaster;
if (vnnmap != NULL) {
talloc_free(vnnmap);
vnnmap = NULL;
}
/* get the recmaster */
ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
exit(10);
}
/* get recovery mode */
ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
exit(10);
}
/* get the current generation number */
ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
exit(10);
}
if ((recmode == CTDB_RECOVERY_NORMAL)
&& (vnnmap->generation != 1)){
return vnnmap->generation;
}
sleep(1);
}
}
/*
ban a node from the cluster
*/
static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
struct ctdb_ban_time bantime;
if (argc < 1) {
usage();
}
/* verify the node exists */
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
return ret;
}
if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
return -1;
}
bantime.pnn = options.pnn;
bantime.time = strtoul(argv[0], NULL, 0);
ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
return -1;
}
ret = control_ipreallocate(ctdb, argc, argv);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
unban a node from the cluster
*/
static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
struct ctdb_ban_time bantime;
/* verify the node exists */
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
return ret;
}
if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
return -1;
}
bantime.pnn = options.pnn;
bantime.time = 0;
ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
return -1;
}
ret = control_ipreallocate(ctdb, argc, argv);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
show ban information for a node
*/
static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct ctdb_node_map *nodemap=NULL;
struct ctdb_ban_time *bantime;
/* verify the node exists */
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
return ret;
}
ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
return -1;
}
if (bantime->time == 0) {
printf("Node %u is not banned\n", bantime->pnn);
} else {
printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
}
return 0;
}
/*
shutdown a daemon
*/
static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
trigger a recovery
*/
static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t generation, next_generation;
/* record the current generation number */
generation = get_generation(ctdb);
ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
return ret;
}
ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
return ret;
}
/* wait until we are in a new generation */
while (1) {
next_generation = get_generation(ctdb);
if (next_generation != generation) {
return 0;
}
sleep(1);
}
return 0;
}
/*
display monitoring mode of a remote node
*/
static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t monmode;
int ret;
ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
return ret;
}
if (!options.machinereadable){
printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
} else {
printf(":mode:\n");
printf(":%d:\n",monmode);
}
return 0;
}
/*
display capabilities of a remote node
*/
static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t capabilities;
int ret;
ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
return ret;
}
if (!options.machinereadable){
printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
} else {
printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
printf(":%d:%d:%d:%d:\n",
!!(capabilities&CTDB_CAP_RECMASTER),
!!(capabilities&CTDB_CAP_LMASTER),
!!(capabilities&CTDB_CAP_LVS),
!!(capabilities&CTDB_CAP_NATGW));
}
return 0;
}
/*
display lvs configuration
*/
static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t *capabilities;
struct ctdb_node_map *nodemap=NULL;
int i, ret;
int healthy_count = 0;
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
return ret;
}
capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
CTDB_NO_MEMORY(ctdb, capabilities);
/* collect capabilities for all connected nodes */
for (i=0; i<nodemap->num; i++) {
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
continue;
}
ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
return ret;
}
if (!(capabilities[i] & CTDB_CAP_LVS)) {
continue;
}
if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
healthy_count++;
}
}
/* Print all LVS nodes */
for (i=0; i<nodemap->num; i++) {
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
continue;
}
if (!(capabilities[i] & CTDB_CAP_LVS)) {
continue;
}
if (healthy_count != 0) {
if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
continue;
}
}
printf("%d:%s\n", i,
ctdb_addr_to_str(&nodemap->nodes[i].addr));
}
return 0;
}
/*
display who is the lvs master
*/
static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t *capabilities;
struct ctdb_node_map *nodemap=NULL;
int i, ret;
int healthy_count = 0;
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
return ret;
}
capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
CTDB_NO_MEMORY(ctdb, capabilities);
/* collect capabilities for all connected nodes */
for (i=0; i<nodemap->num; i++) {
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
continue;
}
ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
return ret;
}
if (!(capabilities[i] & CTDB_CAP_LVS)) {
continue;
}
if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
healthy_count++;
}
}
/* find and show the lvsmaster */
for (i=0; i<nodemap->num; i++) {
if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
continue;
}
if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
continue;
}
if (!(capabilities[i] & CTDB_CAP_LVS)) {
continue;
}
if (healthy_count != 0) {
if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
continue;
}
}
if (options.machinereadable){
printf("%d\n", i);
} else {
printf("Node %d is LVS master\n", i);
}
return 0;
}
printf("There is no LVS master\n");
return -1;
}
/*
disable monitoring on a node
*/
static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
return ret;
}
printf("Monitoring mode:%s\n","DISABLED");
return 0;
}
/*
enable monitoring on a node
*/
static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
return ret;
}
printf("Monitoring mode:%s\n","ACTIVE");
return 0;
}
/*
display remote list of keys/data for a db
*/
static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
{
const char *db_name;
struct ctdb_db_context *ctdb_db;
int ret;
if (argc < 1) {
usage();
}
db_name = argv[0];
if (db_exists(ctdb, db_name)) {
DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
return -1;
}
ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
if (ctdb_db == NULL) {
DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
return -1;
}
/* traverse and dump the cluster tdb */
ret = ctdb_dump_db(ctdb_db, stdout);
if (ret == -1) {
DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
" and 'ctdb getvar AllowUnhealthyDBRead'\n",
db_name));
return -1;
}
talloc_free(ctdb_db);
printf("Dumped %d records\n", ret);
return 0;
}
static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
TDB_DATA data, void *private_data)
{
DEBUG(DEBUG_ERR,("Log data received\n"));
if (data.dsize > 0) {
printf("%s", data.dptr);
}
exit(0);
}
/*
display a list of log messages from the in memory ringbuffer
*/
static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
int32_t res;
struct ctdb_get_log_addr log_addr;
TDB_DATA data;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
char *errmsg;
struct timeval tv;
if (argc != 1) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
talloc_free(tmp_ctx);
return -1;
}
log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
log_addr.srvid = getpid();
if (isalpha(argv[0][0]) || argv[0][0] == '-') {
log_addr.level = get_debug_by_desc(argv[0]);
} else {
log_addr.level = strtol(argv[0], NULL, 0);
}
data.dptr = (unsigned char *)&log_addr;
data.dsize = sizeof(log_addr);
DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
ctdb_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
sleep(1);
DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
if (ret != 0 || res != 0) {
DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
talloc_free(tmp_ctx);
return -1;
}
tv = timeval_current();
/* this loop will terminate when we have received the reply */
while (timeval_elapsed(&tv) < 3.0) {
event_loop_once(ctdb->ev);
}
DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
talloc_free(tmp_ctx);
return 0;
}
/*
clear the in memory log area
*/
static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
int32_t res;
char *errmsg;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
if (ret != 0 || res != 0) {
DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
talloc_free(tmp_ctx);
return -1;
}
talloc_free(tmp_ctx);
return 0;
}
/*
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;
struct ctdb_dbid_map *dbmap=NULL;
ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
return ret;
}
if(options.machinereadable){
printf(":ID:Name:Path:Persistent:Unhealthy:\n");
for(i=0;i<dbmap->num;i++){
const char *path;
const char *name;
const char *health;
bool persistent;
ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, ctdb, &path);
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, ctdb, &name);
ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, ctdb, &health);
persistent = dbmap->dbs[i].persistent;
printf(":0x%08X:%s:%s:%d:%d:\n",
dbmap->dbs[i].dbid, name, path,
!!(persistent), !!(health));
}
return 0;
}
printf("Number of databases:%d\n", dbmap->num);
for(i=0;i<dbmap->num;i++){
const char *path;
const char *name;
const char *health;
bool persistent;
ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
persistent = dbmap->dbs[i].persistent;
printf("dbid:0x%08x name:%s path:%s%s%s\n",
dbmap->dbs[i].dbid, name, path,
persistent?" PERSISTENT":"",
health?" UNHEALTHY":"");
}
return 0;
}
/*
display the status of a database on a remote ctdb
*/
static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
struct ctdb_dbid_map *dbmap=NULL;
const char *db_name;
if (argc < 1) {
usage();
}
db_name = argv[0];
ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
return ret;
}
for(i=0;i<dbmap->num;i++){
const char *path;
const char *name;
const char *health;
bool persistent;
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
if (strcmp(name, db_name) != 0) {
continue;
}
ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
persistent = dbmap->dbs[i].persistent;
printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
dbmap->dbs[i].dbid, name, path,
persistent?"yes":"no",
health?health:"OK");
return 0;
}
DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
return 0;
}
/*
check if the local node is recmaster or not
it will return 1 if this node is the recmaster and 0 if it is not
or if the local ctdb daemon could not be contacted
*/
static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t mypnn, recmaster;
int ret;
mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
if (mypnn == -1) {
printf("Failed to get pnn of node\n");
return 1;
}
ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
if (ret != 0) {
printf("Failed to get the recmaster\n");
return 1;
}
if (recmaster != mypnn) {
printf("this node is not the recmaster\n");
return 1;
}
printf("this node is the recmaster\n");
return 0;
}
/*
ping a node
*/
static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
struct timeval tv = timeval_current();
ret = ctdb_ctrl_ping(ctdb, options.pnn);
if (ret == -1) {
printf("Unable to get ping response from node %u\n", options.pnn);
return -1;
} else {
printf("response from %u time=%.6f sec (%d clients)\n",
options.pnn, timeval_elapsed(&tv), ret);
}
return 0;
}
/*
get a tunable
*/
static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
{
const char *name;
uint32_t value;
int ret;
if (argc < 1) {
usage();
}
name = argv[0];
ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
if (ret == -1) {
DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
return -1;
}
printf("%-19s = %u\n", name, value);
return 0;
}
/*
set a tunable
*/
static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
{
const char *name;
uint32_t value;
int ret;
if (argc < 2) {
usage();
}
name = argv[0];
value = strtoul(argv[1], NULL, 0);
ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
if (ret == -1) {
DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
return -1;
}
return 0;
}
/*
list all tunables
*/
static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t count;
const char **list;
int ret, i;
ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
if (ret == -1) {
DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
return -1;
}
for (i=0;i<count;i++) {
control_getvar(ctdb, 1, &list[i]);
}
talloc_free(list);
return 0;
}
/*
display debug level on a node
*/
static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
int32_t level;
ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
return ret;
} else {
if (options.machinereadable){
printf(":Name:Level:\n");
printf(":%s:%d:\n",get_debug_by_level(level),level);
} else {
printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
}
}
return 0;
}
/*
display reclock file of a node
*/
static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
const char *reclock;
ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
return ret;
} else {
if (options.machinereadable){
if (reclock != NULL) {
printf("%s", reclock);
}
} else {
if (reclock == NULL) {
printf("No reclock file used.\n");
} else {
printf("Reclock file:%s\n", reclock);
}
}
}
return 0;
}
/*
set the reclock file of a node
*/
static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
const char *reclock;
if (argc == 0) {
reclock = NULL;
} else if (argc == 1) {
reclock = argv[0];
} else {
usage();
}
ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
set the natgw state on/off
*/
static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t natgwstate;
if (argc == 0) {
usage();
}
if (!strcmp(argv[0], "on")) {
natgwstate = 1;
} else if (!strcmp(argv[0], "off")) {
natgwstate = 0;
} else {
usage();
}
ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
set the lmaster role on/off
*/
static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t lmasterrole;
if (argc == 0) {
usage();
}
if (!strcmp(argv[0], "on")) {
lmasterrole = 1;
} else if (!strcmp(argv[0], "off")) {
lmasterrole = 0;
} else {
usage();
}
ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
set the recmaster role on/off
*/
static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t recmasterrole;
if (argc == 0) {
usage();
}
if (!strcmp(argv[0], "on")) {
recmasterrole = 1;
} else if (!strcmp(argv[0], "off")) {
recmasterrole = 0;
} else {
usage();
}
ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
return ret;
}
return 0;
}
/*
set debug level on a node or all nodes
*/
static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
int32_t level;
if (argc == 0) {
printf("You must specify the debug level. Valid levels are:\n");
for (i=0; debug_levels[i].description != NULL; i++) {
printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
}
return 0;
}
if (isalpha(argv[0][0]) || argv[0][0] == '-') {
level = get_debug_by_desc(argv[0]);
} else {
level = strtol(argv[0], NULL, 0);
}
for (i=0; debug_levels[i].description != NULL; i++) {
if (level == debug_levels[i].level) {
break;
}
}
if (debug_levels[i].description == NULL) {
printf("Invalid debug level, must be one of\n");
for (i=0; debug_levels[i].description != NULL; i++) {
printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
}
return -1;
}
ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
}
return 0;
}
/*
freeze a node
*/
static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t priority;
if (argc == 1) {
priority = strtol(argv[0], NULL, 0);
} else {
priority = 0;
}
DEBUG(DEBUG_ERR,("Freeze by priority %u\n", priority));
ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, priority);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
}
return 0;
}
/*
thaw a node
*/
static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
uint32_t priority;
if (argc == 1) {
priority = strtol(argv[0], NULL, 0);
} else {
priority = 0;
}
DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
}
return 0;
}
/*
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, false, 0);
if (ctdb_db == NULL) {
DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
return -1;
}
return 0;
}
/*
set db priority
*/
static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
{
struct ctdb_db_priority db_prio;
int ret;
if (argc < 2) {
usage();
}
db_prio.db_id = strtoul(argv[0], NULL, 0);
db_prio.priority = strtoul(argv[1], NULL, 0);
ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
return -1;
}
return 0;
}
/*
get db priority
*/
static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
{
uint32_t db_id, priority;
int ret;
if (argc < 1) {
usage();
}
db_id = strtoul(argv[0], NULL, 0);
ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
return -1;
}
DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
return 0;
}
/*
run an eventscript on a node
*/
static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
{
TDB_DATA data;
int ret;
int32_t res;
char *errmsg;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
if (argc != 1) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
return -1;
}
data.dptr = (unsigned char *)discard_const(argv[0]);
data.dsize = strlen((char *)data.dptr) + 1;
DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
if (ret != 0 || res != 0) {
DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
talloc_free(tmp_ctx);
return -1;
}
talloc_free(tmp_ctx);
return 0;
}
#define DB_VERSION 1
#define MAX_DB_NAME 64
struct db_file_header {
unsigned long version;
time_t timestamp;
unsigned long persistent;
unsigned long size;
const char name[MAX_DB_NAME];
};
struct backup_data {
struct ctdb_marshall_buffer *records;
uint32_t len;
uint32_t total;
bool traverse_error;
};
static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
{
struct backup_data *bd = talloc_get_type(private, struct backup_data);
struct ctdb_rec_data *rec;
/* add the record */
rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
if (rec == NULL) {
bd->traverse_error = true;
DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
return -1;
}
bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
if (bd->records == NULL) {
DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
bd->traverse_error = true;
return -1;
}
bd->records->count++;
memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
bd->len += rec->length;
talloc_free(rec);
bd->total++;
return 0;
}
/*
* backup a database to a file
*/
static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
struct ctdb_dbid_map *dbmap=NULL;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
struct db_file_header dbhdr;
struct ctdb_db_context *ctdb_db;
struct backup_data *bd;
int fh = -1;
int status = -1;
const char *reason = NULL;
if (argc != 2) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
return -1;
}
ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
return ret;
}
for(i=0;i<dbmap->num;i++){
const char *name;
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
if(!strcmp(argv[0], name)){
talloc_free(discard_const(name));
break;
}
talloc_free(discard_const(name));
}
if (i == dbmap->num) {
DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
talloc_free(tmp_ctx);
return -1;
}
ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, tmp_ctx, &reason);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
argv[0]));
talloc_free(tmp_ctx);
return -1;
}
if (reason) {
uint32_t allow_unhealthy = 0;
ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
"AllowUnhealthyDBRead",
&allow_unhealthy);
if (allow_unhealthy != 1) {
DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
argv[0], reason));
DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
allow_unhealthy));
talloc_free(tmp_ctx);
return -1;
}
DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
argv[0], argv[0]));
DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
"tunnable AllowUnhealthyDBRead = %u\n",
allow_unhealthy));
}
ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
if (ctdb_db == NULL) {
DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
talloc_free(tmp_ctx);
return -1;
}
ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
if (ret == -1) {
DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
talloc_free(tmp_ctx);
return -1;
}
bd = talloc_zero(tmp_ctx, struct backup_data);
if (bd == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
talloc_free(tmp_ctx);
return -1;
}
bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
if (bd->records == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
talloc_free(tmp_ctx);
return -1;
}
bd->len = offsetof(struct ctdb_marshall_buffer, data);
bd->records->db_id = ctdb_db->db_id;
/* traverse the database collecting all records */
if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
bd->traverse_error) {
DEBUG(DEBUG_ERR,("Traverse error\n"));
talloc_free(tmp_ctx);
return -1;
}
tdb_transaction_cancel(ctdb_db->ltdb->tdb);
fh = open(argv[1], O_RDWR|O_CREAT, 0600);
if (fh == -1) {
DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
talloc_free(tmp_ctx);
return -1;
}
dbhdr.version = DB_VERSION;
dbhdr.timestamp = time(NULL);
dbhdr.persistent = dbmap->dbs[i].persistent;
dbhdr.size = bd->len;
if (strlen(argv[0]) >= MAX_DB_NAME) {
DEBUG(DEBUG_ERR,("Too long dbname\n"));
goto done;
}
strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
ret = write(fh, &dbhdr, sizeof(dbhdr));
if (ret == -1) {
DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
goto done;
}
ret = write(fh, bd->records, bd->len);
if (ret == -1) {
DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
goto done;
}
status = 0;
done:
if (fh != -1) {
ret = close(fh);
if (ret == -1) {
DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
}
}
talloc_free(tmp_ctx);
return status;
}
/*
* restore a database from a file
*/
static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
TDB_DATA outdata;
TDB_DATA data;
struct db_file_header dbhdr;
struct ctdb_db_context *ctdb_db;
struct ctdb_node_map *nodemap=NULL;
struct ctdb_vnn_map *vnnmap=NULL;
int i, fh;
struct ctdb_control_wipe_database w;
uint32_t *nodes;
uint32_t generation;
struct tm *tm;
char tbuf[100];
if (argc != 1) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
return -1;
}
fh = open(argv[0], O_RDONLY);
if (fh == -1) {
DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
talloc_free(tmp_ctx);
return -1;
}
read(fh, &dbhdr, sizeof(dbhdr));
if (dbhdr.version != DB_VERSION) {
DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
talloc_free(tmp_ctx);
return -1;
}
outdata.dsize = dbhdr.size;
outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
if (outdata.dptr == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
close(fh);
talloc_free(tmp_ctx);
return -1;
}
read(fh, outdata.dptr, outdata.dsize);
close(fh);
tm = localtime(&dbhdr.timestamp);
strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
printf("Restoring database '%s' from backup @ %s\n",
dbhdr.name, tbuf);
ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
if (ctdb_db == NULL) {
DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
talloc_free(tmp_ctx);
return -1;
}
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
talloc_free(tmp_ctx);
return ret;
}
/* freeze all nodes */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
for (i=1; i<=NUM_DB_PRIORITIES; i++) {
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
nodes, i,
TIMELIMIT(),
false, tdb_null,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
}
generation = vnnmap->generation;
data.dptr = (void *)&generation;
data.dsize = sizeof(generation);
/* start a cluster wide transaction */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
return -1;
}
w.db_id = ctdb_db->db_id;
w.transaction_id = generation;
data.dptr = (void *)&w;
data.dsize = sizeof(w);
/* wipe all the remote databases. */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
/* push the database */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
nodes, 0,
TIMELIMIT(), false, outdata,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
data.dptr = (void *)&ctdb_db->db_id;
data.dsize = sizeof(ctdb_db->db_id);
/* mark the database as healthy */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
data.dptr = (void *)&generation;
data.dsize = sizeof(generation);
/* commit all the changes */
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
/* thaw all nodes */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
nodes, 0,
TIMELIMIT(),
false, tdb_null,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
talloc_free(tmp_ctx);
return 0;
}
/*
* dump a database backup from a file
*/
static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
{
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
TDB_DATA outdata;
struct db_file_header dbhdr;
int i, fh;
struct tm *tm;
char tbuf[100];
struct ctdb_rec_data *rec = NULL;
struct ctdb_marshall_buffer *m;
if (argc != 1) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
return -1;
}
fh = open(argv[0], O_RDONLY);
if (fh == -1) {
DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
talloc_free(tmp_ctx);
return -1;
}
read(fh, &dbhdr, sizeof(dbhdr));
if (dbhdr.version != DB_VERSION) {
DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
talloc_free(tmp_ctx);
return -1;
}
outdata.dsize = dbhdr.size;
outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
if (outdata.dptr == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
close(fh);
talloc_free(tmp_ctx);
return -1;
}
read(fh, outdata.dptr, outdata.dsize);
close(fh);
m = (struct ctdb_marshall_buffer *)outdata.dptr;
tm = localtime(&dbhdr.timestamp);
strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
dbhdr.name, m->db_id, tbuf);
for (i=0; i < m->count; i++) {
uint32_t reqid = 0;
TDB_DATA key, data;
/* we do not want the header splitted, so we pass NULL*/
rec = ctdb_marshall_loop_next(m, rec, &reqid,
NULL, &key, &data);
ctdb_dumpdb_record(ctdb, key, data, stdout);
}
printf("Dumped %d records\n", i);
talloc_free(tmp_ctx);
return 0;
}
/*
* wipe a database from a file
*/
static int control_wipedb(struct ctdb_context *ctdb, int argc,
const char **argv)
{
int ret;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
TDB_DATA data;
struct ctdb_db_context *ctdb_db;
struct ctdb_node_map *nodemap = NULL;
struct ctdb_vnn_map *vnnmap = NULL;
int i;
struct ctdb_control_wipe_database w;
uint32_t *nodes;
uint32_t generation;
struct ctdb_dbid_map *dbmap = NULL;
if (argc != 1) {
DEBUG(DEBUG_ERR,("Invalid arguments\n"));
return -1;
}
ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
&dbmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
options.pnn));
return ret;
}
for(i=0;i<dbmap->num;i++){
const char *name;
ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
dbmap->dbs[i].dbid, tmp_ctx, &name);
if(!strcmp(argv[0], name)){
talloc_free(discard_const(name));
break;
}
talloc_free(discard_const(name));
}
if (i == dbmap->num) {
DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
argv[0]));
talloc_free(tmp_ctx);
return -1;
}
ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
if (ctdb_db == NULL) {
DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
argv[0]));
talloc_free(tmp_ctx);
return -1;
}
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
&nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
options.pnn));
talloc_free(tmp_ctx);
return ret;
}
ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
&vnnmap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
options.pnn));
talloc_free(tmp_ctx);
return ret;
}
/* freeze all nodes */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
for (i=1; i<=NUM_DB_PRIORITIES; i++) {
ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
nodes, i,
TIMELIMIT(),
false, tdb_null,
NULL, NULL,
NULL);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
}
generation = vnnmap->generation;
data.dptr = (void *)&generation;
data.dsize = sizeof(generation);
/* start a cluster wide transaction */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL);
if (ret!= 0) {
DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
"transactions.\n"));
return -1;
}
w.db_id = ctdb_db->db_id;
w.transaction_id = generation;
data.dptr = (void *)&w;
data.dsize = sizeof(w);
/* wipe all the remote databases. */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
data.dptr = (void *)&ctdb_db->db_id;
data.dsize = sizeof(ctdb_db->db_id);
/* mark the database as healthy */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
data.dptr = (void *)&generation;
data.dsize = sizeof(generation);
/* commit all the changes */
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
nodes, 0,
TIMELIMIT(), false, data,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
/* thaw all nodes */
nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
nodes, 0,
TIMELIMIT(),
false, tdb_null,
NULL, NULL,
NULL) != 0) {
DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
talloc_free(tmp_ctx);
return -1;
}
talloc_free(tmp_ctx);
return 0;
}
/*
* set flags of a node in the nodemap
*/
static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
int32_t status;
int node;
int flags;
TDB_DATA data;
struct ctdb_node_flag_change c;
if (argc != 2) {
usage();
return -1;
}
if (sscanf(argv[0], "%d", &node) != 1) {
DEBUG(DEBUG_ERR, ("Badly formed node\n"));
usage();
return -1;
}
if (sscanf(argv[1], "0x%x", &flags) != 1) {
DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
usage();
return -1;
}
c.pnn = node;
c.old_flags = 0;
c.new_flags = flags;
data.dsize = sizeof(c);
data.dptr = (unsigned char *)&c;
ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0,
data, NULL, NULL, &status, NULL, NULL);
if (ret != 0 || status != 0) {
DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
return -1;
}
return 0;
}
/*
dump memory usage
*/
static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
{
TDB_DATA data;
int ret;
int32_t res;
char *errmsg;
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
if (ret != 0 || res != 0) {
DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
talloc_free(tmp_ctx);
return -1;
}
write(1, data.dptr, data.dsize);
talloc_free(tmp_ctx);
return 0;
}
/*
handler for memory dumps
*/
static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
TDB_DATA data, void *private_data)
{
write(1, data.dptr, data.dsize);
exit(0);
}
/*
dump memory usage on the recovery daemon
*/
static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
{
int ret;
TDB_DATA data;
struct rd_memdump_reply rd;
rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
if (rd.pnn == -1) {
DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
return -1;
}
rd.srvid = getpid();
/* register a message port for receiveing the reply so that we
can receive the reply
*/
ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
data.dptr = (uint8_t *)&rd;
data.dsize = sizeof(rd);
ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
return -1;
}
/* this loop will terminate when we have received the reply */
while (1) {
event_loop_once(ctdb->ev);
}
return 0;
}
/*
list all nodes in the cluster
if the daemon is running, we read the data from the daemon.
if the daemon is not running we parse the nodes file directly
*/
static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
struct ctdb_node_map *nodemap=NULL;
if (ctdb != NULL) {
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
return ret;
}
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
continue;
}
if (options.machinereadable){
printf(":%d:%s:\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
} else {
printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
}
}
} else {
TALLOC_CTX *mem_ctx = talloc_new(NULL);
struct pnn_node *pnn_nodes;
struct pnn_node *pnn_node;
pnn_nodes = read_nodes_file(mem_ctx);
if (pnn_nodes == NULL) {
DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
talloc_free(mem_ctx);
return -1;
}
for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
ctdb_sock_addr addr;
if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
talloc_free(mem_ctx);
return -1;
}
if (options.machinereadable){
printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
} else {
printf("%s\n", pnn_node->addr);
}
}
talloc_free(mem_ctx);
}
return 0;
}
/*
reload the nodes file on the local node
*/
static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
{
int i, ret;
int mypnn;
struct ctdb_node_map *nodemap=NULL;
mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
if (mypnn == -1) {
DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
return -1;
}
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
return ret;
}
/* reload the nodes file on all remote nodes */
for (i=0;i<nodemap->num;i++) {
if (nodemap->nodes[i].pnn == mypnn) {
continue;
}
DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
nodemap->nodes[i].pnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
}
}
/* reload the nodes file on the local node */
DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
}
/* initiate a recovery */
control_recover(ctdb, argc, argv);
return 0;
}
static const struct {
const char *name;
int (*fn)(struct ctdb_context *, int, const char **);
bool auto_all;
bool without_daemon; /* can be run without daemon running ? */
const char *msg;
const char *args;
} ctdb_commands[] = {
#ifdef CTDB_VERS
{ "version", control_version, true, false, "show version of ctdb" },
#endif
{ "status", control_status, true, false, "show node status" },
{ "uptime", control_uptime, true, false, "show node uptime" },
{ "ping", control_ping, true, false, "ping all nodes" },
{ "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
{ "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
{ "listvars", control_listvars, true, false, "list tunable variables"},
{ "statistics", control_statistics, false, false, "show statistics" },
{ "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
{ "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
{ "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
{ "getdbmap", control_getdbmap, true, false, "show the database map" },
{ "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
{ "catdb", control_catdb, true, false, "dump a database" , "<dbname>"},
{ "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
{ "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
{ "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
{ "lvs", control_lvs, true, false, "show lvs configuration" },
{ "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
{ "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
{ "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
{ "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
{ "getdebug", control_getdebug, true, false, "get debug level" },
{ "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
{ "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
{ "attach", control_attach, true, false, "attach to a database", "<dbname>" },
{ "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
{ "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
{ "getpid", control_getpid, true, false, "get ctdbd process ID" },
{ "disable", control_disable, true, false, "disable a nodes public IP" },
{ "enable", control_enable, true, false, "enable a nodes public IP" },
{ "stop", control_stop, true, false, "stop a node" },
{ "continue", control_continue, true, false, "re-start a stopped node" },
{ "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
{ "unban", control_unban, true, false, "unban a node" },
{ "showban", control_showban, true, false, "show ban information"},
{ "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
{ "recover", control_recover, true, false, "force recovery" },
{ "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
{ "freeze", control_freeze, true, false, "freeze databases", "[priority:1-3]" },
{ "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
{ "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
{ "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
{ "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
{ "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
{ "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip>" },
{ "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
{ "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
{ "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
{ "getsrvids", getsrvids, false, false, "get a list of all server ids"},
{ "vacuum", ctdb_vacuum, false, false, "vacuum the databases of empty records", "[max_records]"},
{ "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
{ "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
{ "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
{ "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
{ "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
{ "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
{ "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
{ "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
{ "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file>"},
{ "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
{ "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
{ "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
{ "setflags", control_setflags, false, false, "set flags for a node in the nodemap.", "<node> <flags>"},
{ "scriptstatus", control_scriptstatus, false, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
{ "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
{ "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
{ "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
{ "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
{ "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
{ "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
{ "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
{ "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
{ "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
{ "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
{ "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
};
/*
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"
" -Y generate machinereadable output\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 %-27s %s\n",
ctdb_commands[i].name,
ctdb_commands[i].args?ctdb_commands[i].args:"",
ctdb_commands[i].msg);
}
exit(1);
}
static void ctdb_alarm(int sig)
{
printf("Maximum runtime exceeded - exiting\n");
_exit(ERR_TIMEOUT);
}
/*
main program
*/
int main(int argc, const char *argv[])
{
struct ctdb_context *ctdb;
char *nodestring = NULL;
struct poptOption popt_options[] = {
POPT_AUTOHELP
POPT_CTDB_CMDLINE
{ "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
{ "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
{ "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
{ "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
POPT_TABLEEND
};
int opt;
const char **extra_argv;
int extra_argc = 0;
int ret=-1, i;
poptContext pc;
struct event_context *ev;
const char *control;
setlinebuf(stdout);
/* set some defaults */
options.maxruntime = 0;
options.timelimit = 3;
options.pnn = CTDB_CURRENT_NODE;
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
while ((opt = poptGetNextOpt(pc)) != -1) {
switch (opt) {
default:
DEBUG(DEBUG_ERR, ("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();
}
if (options.maxruntime == 0) {
const char *ctdb_timeout;
ctdb_timeout = getenv("CTDB_TIMEOUT");
if (ctdb_timeout != NULL) {
options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
} else {
/* default timeout is 120 seconds */
options.maxruntime = 120;
}
}
signal(SIGALRM, ctdb_alarm);
alarm(options.maxruntime);
/* setup the node number to contact */
if (nodestring != NULL) {
if (strcmp(nodestring, "all") == 0) {
options.pnn = CTDB_BROADCAST_ALL;
} else {
options.pnn = strtoul(nodestring, NULL, 0);
}
}
control = extra_argv[0];
ev = event_context_init(NULL);
for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
if (strcmp(control, ctdb_commands[i].name) == 0) {
int j;
if (ctdb_commands[i].without_daemon == true) {
close(2);
}
/* initialise ctdb */
ctdb = ctdb_cmdline_client(ev);
if (ctdb_commands[i].without_daemon == false) {
if (ctdb == NULL) {
DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
exit(1);
}
/* verify the node exists */
verify_node(ctdb);
if (options.pnn == CTDB_CURRENT_NODE) {
int pnn;
pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
if (pnn == -1) {
return -1;
}
options.pnn = pnn;
}
}
if (ctdb_commands[i].auto_all &&
options.pnn == 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 (j=0;j<num_nodes;j++) {
options.pnn = nodes[j];
ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
}
talloc_free(nodes);
} else {
ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
}
break;
}
}
if (i == ARRAY_SIZE(ctdb_commands)) {
DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
exit(1);
}
return ret;
}
|