core.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * tt管理工具
  4. * 作者:于斌
  5. */
  6. #tt守护进程核心
  7. define('TT_IN' , true);
  8. define('TT' , 'tt');
  9. define('TT_PATH' , dirname(__FILE__) . DIRECTORY_SEPARATOR);
  10. define('TT_SERVICE_PATH' , TT_PATH . 'service' . DIRECTORY_SEPARATOR);
  11. define('TT_DB_PATH' , TT_PATH . 'db' . DIRECTORY_SEPARATOR);
  12. define('TT_BAK_PATH' , TT_PATH . 'bak' . DIRECTORY_SEPARATOR);
  13. define('TT_MANAGE_PATH' , TT_PATH . 'manage' . DIRECTORY_SEPARATOR);
  14. define('TT_CONFIG_PATH' , TT_PATH . 'config' . DIRECTORY_SEPARATOR);
  15. define('TT_LOG_PATH' , TT_PATH . 'log' . DIRECTORY_SEPARATOR);
  16. /**
  17. * tt_config()
  18. */
  19. function tt_config()
  20. {
  21. return scandir(TT_CONFIG_PATH);
  22. }
  23. /**
  24. * tt_echo()
  25. */
  26. function tt_echo($string)
  27. {
  28. static $str;
  29. $str .= tt_log($string . "\n");
  30. return $str;
  31. }
  32. /**
  33. * tt_log()
  34. */
  35. function tt_log($str)
  36. {
  37. $time = time();
  38. $file = tt_mkdir(TT_LOG_PATH . date('Y-m-d', $time) . '/') . date('Y-m-d_H', $time) . '.log';
  39. $content = '';
  40. if(is_file($file))
  41. {
  42. $content = file_get_contents($file);
  43. }
  44. $content .= date('Y-m-d_H:i:s', $time) . ':' . $str;
  45. file_put_contents($file, $content);
  46. return $str;
  47. }
  48. /**
  49. * tt_system()
  50. */
  51. function tt_system($command)
  52. {
  53. system($command);
  54. }
  55. /**
  56. * tt_create()
  57. */
  58. function tt_create($config)
  59. {
  60. if(isset($config['host']))
  61. {
  62. $command = $config['server'] . 'ttserver -host ' . $config['host'] . ' -port ' . $config['port'] .' -thnum ' . $config['thnum'] .' -dmn -pid ' . $config['path'] . $config['name'] . '.pid -log ' . $config['path'] . $config['name'] . '.log -le -ulog ' . $config['ulog'] . ' -ulim ' . $config['ulim'] . ' -rts ' . $config['path'] . $config['name'] . '.rts ';
  63. if(isset($config['mhost']))
  64. {
  65. $command .= '-sid ' . $config['sid'] . ' -mhost ' . $config['mhost'] . ' -mport ' . $config['mport'] . ' ';
  66. }
  67. if($config['type'] == '*')
  68. {
  69. $command .= '*';
  70. }
  71. else
  72. {
  73. $command .= $config['path'] . $config['name'] . '.' . $config['type'] . $config['ext'];
  74. }
  75. $command = 'ulimit -SHn 51200;' . escapeshellcmd($command);
  76. tt_system($command);
  77. }
  78. }
  79. /**
  80. * tt_mkdir()
  81. */
  82. function tt_mkdir($dir)
  83. {
  84. if(!is_dir($dir))
  85. {
  86. mkdir($dir, 0775);
  87. }
  88. return $dir;
  89. }
  90. /**
  91. * tt_runtime()
  92. */
  93. function tt_runtime()
  94. {
  95. static $time;
  96. if(!$time)
  97. {
  98. $time = microtime();
  99. }
  100. else
  101. {
  102. $new = microtime() - $time;
  103. return $new;
  104. }
  105. }
  106. /**
  107. * tt_load()
  108. */
  109. function tt_load($php, $func = 'start')
  110. {
  111. if(strstr($php, '.php') && is_file(TT_CONFIG_PATH . $php))
  112. {
  113. include(TT_CONFIG_PATH . $php);
  114. if($config['global']['state'] == 1)
  115. {
  116. $path = tt_mkdir(TT_DB_PATH . $config['global']['name'] . '/');
  117. if(!is_file($path . 'create'))
  118. {
  119. tt_echo($config['global']['name'] . ':正在对相关服务进行指定操作');
  120. if(isset($config['db']))
  121. {
  122. foreach($config['db'] as $i => $j)
  123. {
  124. call_user_func('tt_' . $func, $config, $j);
  125. }
  126. tt_echo($config['global']['name'] . ':完成指定操作');
  127. }
  128. else
  129. {
  130. tt_echo($config['global']['name'] . ':没有数据库需要启动');
  131. }
  132. file_put_contents($path . 'create', time());
  133. }
  134. else
  135. {
  136. if(is_string($func))
  137. {
  138. unlink($path . 'create');
  139. tt_echo($config['global']['name'] . ':清理权限文件成功');
  140. tt_load($php, $func);
  141. }
  142. else
  143. {
  144. tt_echo($config['global']['name'] . ':没有操作权限');
  145. }
  146. }
  147. }
  148. else
  149. {
  150. tt_echo($config['global']['name'] . ':服务已关闭');
  151. }
  152. }
  153. elseif(!strstr($php, '.'))
  154. {
  155. tt_echo($php . ':文件不存在,创建失败');
  156. }
  157. }
  158. /**
  159. * tt_start()
  160. */
  161. function tt_start($data, $name)
  162. {
  163. $path = tt_mkdir(TT_DB_PATH . $data['global']['name'] . '/' . $name . '/');
  164. if(isset($data[$name]) && !is_file($path . $name . '.pid'))
  165. {
  166. $data[$name]['name'] = $name;
  167. $data[$name]['path'] = $path;
  168. $data[$name]['ulog'] = tt_mkdir($path . 'ulog/');
  169. $data[$name]['server'] = $data['global']['server'];
  170. tt_create($data[$name]);
  171. $file = $path . $name . '.' . $data[$name]['type'];
  172. if(is_file($file) && !is_file($file . '.create'))
  173. {
  174. $command = 'cp ' . $file . ' ' . $file . '.create';
  175. tt_system($command);
  176. }
  177. tt_echo($data['global']['name'] . ':' . $name . '数据库启动成功');
  178. }
  179. else
  180. {
  181. tt_echo($data['global']['name'] . ':' . $name . '数据库已启动。');
  182. }
  183. }
  184. /**
  185. * tt_del()
  186. */
  187. function tt_del($data, $name)
  188. {
  189. $file = tt_mkdir(TT_DB_PATH . $data['global']['name'] . '/' . $name . '/') . $name . '.' . $data[$name]['type'];
  190. if(is_file($file) && is_file($file . '.create'))
  191. {
  192. $command = 'rm -rf ' . $file . ';cp ' . $file . '.create ' . $file;
  193. tt_system($command);
  194. tt_echo($data['global']['name'] . ':' . $name . '数据库清理成功');
  195. }
  196. elseif(is_file($file . '.create'))
  197. {
  198. $command = 'cp ' . $file . '.create ' . $file;
  199. tt_system($command);
  200. tt_echo($data['global']['name'] . ':' . $name . '数据库清理成功');
  201. }
  202. else
  203. {
  204. tt_echo($data['global']['name'] . ':' . $name . '数据库清理失败。');
  205. }
  206. }
  207. /**
  208. * tt_bak()
  209. */
  210. function tt_bak($data, $name)
  211. {
  212. $dir = tt_mkdir(TT_DB_PATH . $data['global']['name'] . '/' . $name . '/');
  213. if(is_dir($dir))
  214. {
  215. //tt_bakdel($data, $name);
  216. $bak = tt_mkdir(tt_mkdir(TT_BAK_PATH . $data['global']['name']) . '/' . $name . '/') . date('Y-m-d_H:i:s', time()) . '.tar.gz';
  217. $pid = $dir . $name . '.pid';
  218. $command = "tar --exclude $pid -zcvPf $bak $dir";
  219. tt_system($command);
  220. tt_echo($data['global']['name'] . ':' . $name . '数据库备份成功。');
  221. }
  222. else
  223. {
  224. tt_echo($data['global']['name'] . ':' . $name . '数据库目录不存在,无法备份。');
  225. }
  226. }
  227. /**
  228. * tt_bakdel()
  229. */
  230. function tt_bakdel($data, $name)
  231. {
  232. $dir = TT_BAK_PATH . $data['global']['name'] . '/' . $name . '/';
  233. if(is_dir($dir))
  234. {
  235. $command = "rm -rf {$dir}*.tar.gz";
  236. tt_system($command);
  237. tt_echo($data['global']['name'] . ':' . $name . '备份删除成功。');
  238. }
  239. else
  240. {
  241. tt_echo($data['global']['name'] . ':' . $name . '备份目录不存在,无法删除。');
  242. }
  243. }
  244. /**
  245. * tt_bakroll()
  246. */
  247. function tt_bakroll($data, $name)
  248. {
  249. $dir = TT_BAK_PATH . $data['global']['name'] . '/' . $name . '/';
  250. if(is_dir($dir))
  251. {
  252. $file = scandir($dir);
  253. $num = count($file) - 1;
  254. $bak = $dir . $file[$num];
  255. $command = "tar -zxvf $bak";
  256. tt_system($command);
  257. tt_echo($data['global']['name'] . ':' . $name . '备份回滚成功。');
  258. }
  259. else
  260. {
  261. tt_echo($data['global']['name'] . ':' . $name . '备份目录不存在,无法回滚。');
  262. }
  263. }
  264. /**
  265. * tt_ulog()
  266. */
  267. function tt_ulog($data, $name)
  268. {
  269. $dir = TT_DB_PATH . $data['global']['name'] . '/' . $name . '/' . 'ulog/';
  270. if(is_dir($dir))
  271. {
  272. $ulog = scandir($dir);
  273. foreach($ulog as $i => $j)
  274. {
  275. $num[$i] = intval(str_replace('.ulog', '', $j));
  276. $file[$i] = $dir . $j;
  277. $next[$i] = $dir . str_pad(($num[$i]+1), 8, '0', STR_PAD_LEFT) . '.ulog';
  278. if(is_file($next[$i]))
  279. {
  280. unlink($file[$i]);
  281. }
  282. }
  283. tt_echo($data['global']['name'] . ':' . $name . 'ulog清理成功,只留下最新的日志。');
  284. }
  285. else
  286. {
  287. //tt_echo($data['global']['name'] . ':' . $name . 'ulog清理失败。');
  288. }
  289. }
  290. /**
  291. * tt_stop()
  292. */
  293. function tt_stop($data, $name)
  294. {
  295. $file = tt_mkdir(TT_DB_PATH . $data['global']['name'] . '/' . $name . '/') . $name . '.pid';
  296. if(is_file($file))
  297. {
  298. $pid = file_get_contents($file);
  299. @unlink($file);
  300. $command = 'kill -15 ' . $pid;
  301. tt_system($command);
  302. tt_echo($data['global']['name'] . ':' . $name . '数据库关闭成功。');
  303. }
  304. else
  305. {
  306. tt_echo($data['global']['name'] . ':' . $name . '数据库已关闭。');
  307. }
  308. }
  309. /**
  310. * tt_restart()
  311. */
  312. function tt_restart($data, $name)
  313. {
  314. tt_stop($data, $name);
  315. tt_start($data, $name);
  316. }