diff --git a/model/mDaemon.php b/model/mDaemon.php deleted file mode 100644 index 504334f..0000000 --- a/model/mDaemon.php +++ /dev/null @@ -1,635 +0,0 @@ -obj = new dDaemon(); - - $this->ips = 'daemon_server_ips'; - $this->daemon = 'daemon_list'; - $this->dconf = 'daemon_conf'; - - $this->proxy_list = 'proxy_list'; - $this->proxy_exec_list = 'proxy_exec_list'; - - $this->rd_slb_list = "rd_slb_list"; - } - - /** - * 新增服务器 - * @param unknown $ip 服务器内网ip - * @return boolean|boolean|string - */ - public function addServerIp($ip, $remark) { - if(empty($ip)) { - $this->setError('服务器内网IP不能为空'); - return false; - } - - $is_exist = $this->getServerIpInfo($ip); - if($is_exist) { - $this->setError('服务器已存在'); - return false; - } - - $res = $this->obj->insert($this->ips, array('ip'=>$ip, 'remark'=>$remark)); - if(!$res) { - $this->setError('服务器添加到数据库失败'); - return false; - } - return $res; - } - - public function updateServerIp($ip, $remark) { - $is_exist = $this->getServerIpInfo($ip); - if(!$is_exist) { - $this->setError('服务器不存在'); - return false; - } - - return $this->obj->update($this->ips, array('remark'=>$remark), array('sql'=>'`ip`=?', 'vals'=>array($ip))); - } - - /** - * 获取所有服务器 - */ - public function getServerIps() { - return $this->obj->selectAll($this->ips, array()); - } - - /** - * 去除服务器 - * @param unknown $ip - */ - public function delServerIp($ip) { - if(empty($ip)) { - $this->setError('服务器内网IP不能为空'); - return false; - } - - $is_exist = $this->getServerIpInfo($ip); - - // 删除server_ips表数据 - $res = $this->obj->delete($this->ips, array('sql'=>'`id`=? and `ip`=?', 'vals'=>array($is_exist['id'], $ip))); - if(_RC_SERVER_IP_OPEN && $res) { - $delres = $this->delRedisCache(sprintf(_RC_SERVER_IP, $ip)); - } - - // 删除daemon_conf表数据 - if(_RC_DAEMON_CONF_OPEN) { - $list = $this->getDaemonConfList($is_exist['id']); - foreach ($list as $info) { - if(empty($info)) continue; - - $rckey = sprintf(_RC_KL_DAEMON_CONF_BY_SID_DID, $is_exist['id'], $info['daemon_id']); - $rc_is_exist = $this->getRedisCache($rckey); - if(!$rc_is_exist) continue; - - $delres = $this->delRedisCache($rckey); - - } - } - $res = $this->obj->delete($this->dconf, array('sql'=>'`server_id`=?', 'vals'=>array($is_exist['id']))); - return true; - } - - /** - * 通过服务器内网ip获取服务器信息 - * @param unknown $ip - */ - public function getServerIpInfo($ip) { - if(empty($ip)) { - $this->setError('服务器内网IP不能为空'); - return false; - } - - if(_RC_SERVER_IP_OPEN) { - $key = sprintf(_RC_SERVER_IP, $ip); - $info = $this->getRedisCache($key); - if($info != false) return $info; - } - - $info = $this->obj->select($this->ips, array('sql'=>'`ip`=?', 'vals'=>array($ip))); - if(_RC_SERVER_IP_OPEN && $info) { - $res = $this->setRedisCache($key, 600, $info); - } - return $info; - } - - /** - * 新增进程配置 - * @param unknown $server_id 服务器对应id - * @param unknown $daemon_id 进程对应id - * @param unknown $maxnum 启用最大进程数 - * @param number $maxtime 单任务允许最大时长(秒) - */ - public function addDaemonConf($server_id, $daemon_id, $maxnum, $maxtime=0) { - if(empty($server_id)) { - $this->setError('服务器id不能为空'); - return false; - } - if(empty($daemon_id)) { - $this->setError('进程id不能为空'); - return false; - } - - $is_exist = $this->getDaemonConf($server_id, $daemon_id); - if($is_exist) { - $res = $this->updateDaemonConf($server_id, $daemon_id, $maxnum, $maxtime); - if(!$res) { - $this->setError('保存失败'); - return false; - } - return true; - } - - $data['server_id'] = $server_id; - $data['daemon_id'] = $daemon_id; - $data['maxnum'] = $maxnum; - if($maxtime) $data['maxtime'] = $maxtime; - - return $this->obj->insert($this->dconf, $data); - } - - /** - * 获取进程配置信息 - * @param unknown $server_id 服务器对应id - * @param unknown $daemon_id 进程对应id - */ - public function getDaemonConf($server_id, $daemon_id) { - if(_RC_DAEMON_CONF_OPEN) { - $key = sprintf(_RC_KL_DAEMON_CONF_BY_SID_DID, $server_id, $daemon_id); - $info = $this->getRedisCache($key); - if($info != false) return $info; - } - - $info = $this->obj->select($this->dconf, array('sql'=>'`server_id`=? and `daemon_id`=?', 'vals'=>array($server_id, $daemon_id))); - if(_RC_DAEMON_CONF_OPEN && $info) { - $res = $this->setRedisCache($key, 600, $info); - } - return $info; - } - - /** - * 更新进程配置 - * @param unknown $server_id 服务器对应id - * @param unknown $daemon_id 进程对应id - * @param unknown $maxnum 启用最大进程数 - * @param unknown $maxtime 单任务允许最大时长(秒) - * @return boolean - */ - public function updateDaemonConf($server_id, $daemon_id, $maxnum, $maxtime) { - $res = $this->obj->update($this->dconf, array('maxnum'=>$maxnum, 'maxtime'=>$maxtime), array('sql'=>'`server_id`=? and `daemon_id`=?', 'vals'=>array($server_id, $daemon_id))); - if(_RC_DAEMON_CONF_OPEN && $res) { - $key = sprintf(_RC_KL_DAEMON_CONF_BY_SID_DID, $server_id, $daemon_id); - $delres = $this->delRedisCache($key); - } - return $res; - } - - public function updateDaemonConfByIds($ids, $server_id) { - return $this->obj->updateIn($this->dconf, array('server_id'=>$server_id), array('id'=>$ids)); - } - - /** - * 获取服务器对应id 进程对应id 启用进程数关系 - * @return unknown - */ - public function getServerid2Daemon2Info() { - $list = $this->obj->selectAll($this->dconf, array()); - foreach($list as $info) { - $nlist[$info['server_id']][$info['daemon_id']] = $info; - } - return $nlist; - } - - /** - * 获取所有/某服务器 所有进程/某进程 配置 - * @param number $server_id 服务器对应id - * @param number $daemon_id 进程对应id - */ - public function getDaemonConfList($server_id=0, $daemon_id=0) { - $where['sql'] = '1=1'; - $where['vals'] = array(); - - if($server_id) { - $where['sql'] .= ' and `server_id`=?'; - $where['vals'][] = $server_id; - } - - if($daemon_id) { - $where['sql'] .= ' and `daemon_id`=?'; - $where['vals'][] = $daemon_id; - } - return $this->obj->selectAll($this->dconf, $where); - } - - - /** - * 新增进程 - * @param unknown $flag 进程标识 - * @param unknown $proc 进程执行路径 - * @param unknown $desc 进程说明 - */ - public function addDaemon($flag, $proc, $desc) { - if(empty($flag)) { - $this->setError('进程标识不能为空'); - return false; - } - if(empty($proc)) { - $this->setError('进程路径不能为空'); - return false; - } - if(empty($desc)) { - $this->setError('进程说明不能为空'); - return false; - } - - $is_exist = $this->getDaemonByFlag($flag); - if($is_exist) { - $this->setError('进程标识已存在'); - return false; - } - - $data['flag'] = $flag; - $data['proc'] = $proc; - $data['desc'] = $desc; - return $this->obj->insert($this->daemon, $data); - } - - /** - * 批量获取进程列表 通过进程ids - * @param unknown $daemon_ids 进程id数组 array(id1,id2...) - */ - public function getDaemonListByIds($daemon_ids) { - return $this->obj->selectIn($this->daemon, array('id'=>$daemon_ids)); - } - - /** - * 通过进程标识获取进程信息 - * @param unknown $flag 进程标识 - */ - public function getDaemonByFlag($flag) { - if(_RC_DAEMON_OPEN) { - $key = sprintf(_RC_KL_DAEMON_BY_FLAG, $flag); - $info = $this->getRedisCache($key); - if($info != false) return $info; - } - $info = $this->obj->select($this->daemon, array('sql'=>'`flag`=?', 'vals'=>array($flag))); - if(_RC_DAEMON_OPEN && $info) { - $res = $this->setRedisCache($key, 600, $info); - } - return $info; - } - - /** - * 通过进程id获取进程信息 - * @param unknown $id 进程id - * @return boolean|mixed - */ - public function getDaemonById($id) { - if(_RC_DAEMON_OPEN) { - $key = sprintf(_RC_KL_DAEMON_BY_ID, $id); - $info = $this->getRedisCache($key); - if($info != false) return $info; - } - - $info = $this->obj->select($this->daemon, array('sql'=>'`id`=?', 'vals'=>array($id))); - if(_RC_DAEMON_OPEN && $info) { - $res = $this->setRedisCache($key, 600, $info); - } - return $info; - } - - /** - * 更新进程信息 - * @param unknown $id - * @param unknown $data - */ - public function updateDaemon($id, $data) { - $res = $this->obj->update($this->daemon, $data, array('sql'=>'`id`=?', 'vals'=>array($id))); - if(_RC_DAEMON_OPEN && $res) { - $key = sprintf(_RC_KL_DAEMON_BY_ID, $id); - $delres = $this->delRedisCache($key); - } - return $res; - } - - /** - * 获取所有进程列表 - * @return boolean - */ - public function getDaemonList() { - return $this->obj->selectAll($this->daemon); - } - - /** - * 删除进程 - * @param unknown $daemon_id - */ - public function delDaemon($daemon_id) { - $dinfo = $this->getDaemonById($daemon_id); - - // 删除daemon_list表数据 - if(_RC_DAEMON_OPEN) { - $delres = $this->delRedisCache(sprintf(_RC_KL_DAEMON_BY_ID, $daemon_id)); - $delres = $this->delRedisCache(sprintf(_RC_KL_DAEMON_BY_FLAG, $dinfo['flag'])); - } - $res = $this->obj->delete($this->daemon, array('sql'=>'`id`=?', 'vals'=>array($daemon_id))); - - // 删除daemon_conf表数据 - if(_RC_DAEMON_CONF_OPEN) { - $list = $this->getDaemonConfList(0, $daemon_id); - foreach ($list as $info) { - if(empty($info)) continue; - - $rckey = sprintf(_RC_KL_DAEMON_CONF_BY_SID_DID, $info['server_id'], $info['daemon_id']); - $rc_is_exist = $this->getRedisCache($rckey); - if(!$rc_is_exist) continue; - - $delres = $this->delRedisCache($rckey); - } - } - $this->obj->delete($this->dconf, array('sql'=>'`daemon_id`=?', 'vals'=>array($daemon_id))); - return true; - } - - /** - * 获取某进程启用进程数 - * @param unknown $server_ip 服务器对应id - * @param unknown $daemon 进程标识 - */ - public function getDaemonNum($server_ip, $daemon_flag) { - $ipinfo = $this->getServerIpInfo($server_ip); - $daemon_inf = $this->getDaemonByFlag($daemon_flag); - $res = $this->getDaemonConf($ipinfo['id'], $daemon_inf['id']); - return $res['maxnum']+0; - } - - /** - * 获取开启进程数据 - * @param unknown $server_ip - */ - public function getStartProcConf($server_ip) { - $server_inf = $this->getServerIpInfo($server_ip); - - $list = $this->getDaemonConfList($server_inf['id']); - foreach ($list as $conf) { - $daemonids[] = $conf['daemon_id']; - $daemonid2maxnum[$conf['daemon_id']] = $conf['maxnum']; - $daemonid2maxtime[$conf['daemon_id']] = $conf['maxtime']; - } - $daemon_list = $this->getDaemonListByIds($daemonids); - foreach ($daemon_list as $key => $daemon) { - $flag2proc[$daemon['flag']] = $daemon['proc']; - $flag2maxnum[$daemon['flag']] = $daemonid2maxnum[$daemon['id']]; - $flag2maxtime[$daemon['flag']] = $daemonid2maxtime[$daemon['id']]; - } - $nlist['flag2proc'] = $flag2proc; - $nlist['flag2maxnum'] = $flag2maxnum; - $nlist['flag2maxtime'] = $flag2maxtime; - return $nlist; - } - - /** - * 添加代理信息 - * @param unknown $server_ip - * @param unknown $server_port - * @param unknown $server_passwd - * @param unknown $client_port - * @param string $remark - * @return boolean|boolean|string - */ - public function addProxy($server_ip, $server_port, $server_passwd, $client_port, $remark='') { - if(empty($server_ip)) { - $this->setError('服务器地址不能为空'); - return false; - } - $is_exist = $this->getProxyByServerip($server_ip); - if($is_exist) { - $this->setError('服务器地址已经存在'); - return false; - } - - if(empty($server_port)) { - $this->setError('服务器端口不能为空'); - return false; - } - if(empty($server_passwd)) { - $this->setError('服务器密码不能为空'); - return false; - } - if(empty($client_port)) { - $this->setError('客户端端口不能为空'); - return false; - } - - $data['server_ip'] = trim($server_ip); - $data['server_port'] = $server_port; - $data['server_passwd'] = trim($server_passwd); - $data['client_port'] = $client_port; - if(!empty($remark)) $data['remark'] = trim($remark); - - return $this->obj->insert($this->proxy_list, $data); - } - - /** - * 更新代理信息 - * @param unknown $id - * @param array $data - * @return boolean - */ - public function updateProxy($id, $data=array()) { - if(empty($id)) { - $this->setError('代理id不能为空'); - return false; - } - if(empty($data)) { - $this->setError('更新信息不能为空'); - return false; - } - $proxy_inf = $this->getProxyById($id); - if(empty($proxy_inf)) { - $this->setError('代理信息不存在'); - return false; - } - if(isset($data['server_ip'])) { - $is_exist = $this->getProxyByServerip($data['server_ip']); - if($is_exist && $id != $is_exist['id']) { - $this->setError('服务器地址已经存在'); - return false; - } - } - - return $this->obj->update($this->proxy_list, $data, array('sql'=>'`id`=?', 'vals'=>array($id))); - } - - public function getProxyByServerip($ip) { - return $this->obj->select($this->proxy_list, array('sql'=>'`server_ip`=?', 'vals'=>array($ip))); - } - - /** - * 根据代理id获取代理信息 - * @param unknown $id - * @return boolean|boolean|mixed - */ - public function getProxyById($id) { - if(empty($id)) { - $this->setError('代理id不能为空'); - return false; - } - return $this->obj->select($this->proxy_list, array('sql'=>'`id`=?', 'vals'=>array($id))); - } - - /** - * 根据代理id获取代理信息 - * @param unknown $id - * @return boolean|boolean|mixed - */ - public function getProxyList() { - return $this->obj->selectAll($this->proxy_list, array(), 'remark'); - } - - /** - * 根据多代理id查询代理信息 - * @param unknown $ids - * @return boolean|boolean - */ - public function getProxyListByIds($ids) { - if(empty($ids)) { - $this->setError('代理id数组不存在'); - return false; - } - return $this->obj->selectIn($this->proxy_list, array('id'=>$ids)); - } - - public function delProxy($id) { - if(empty($id)) { - $this->setError('代理id不能为空'); - return false; - } - $proxy_inf = $this->getProxyById($id); - if(empty($proxy_inf)) { - $this->setError('代理信息不存在'); - return false; - } - - $sqls[] = 'DELETE FROM '.$this->proxy_list.' WHERE `id`='.$id; - $sqls[] = 'DELETE FROM '.$this->proxy_exec_list.' WHERE `pid`='.$id; - - $res = $this->obj->execTrans($sqls); - if(!$res) { - $this->setError('删除失败'); - return false; - } - return true; - } - - public function getMaxClientPort() { - $sql = "SELECT MAX(`client_port`) as port FROM `{$this->proxy_list}`"; - $res = $this->obj->execute($sql, true, true); - return $res[0]['port']; - } - - public function getClientPort($ori_client_port) { - if (!$ori_client_port) return false; - - $port = $ori_client_port+1; - - $cmd = "ps aux|grep {$port} |grep -v grep| wc -l"; - $res = trim(shell_exec($cmd)); - if ($res) $this->getClientPort($port); - - return $port; - } - - - public function isStartProxy($pid) { - if($pid+0<=0) { - $this->setError('代理id为空'); - return false; - } - - $proxy_inf = $this->getProxyById($pid); - if(empty($proxy_inf)) { - $this->setError('代理信息不存在'); - return false; - } - - $is_start_proxy = $this->startProxyProcess($proxy_inf['server_ip'], $proxy_inf['server_port'], $proxy_inf['client_port'], $proxy_inf['server_passwd']); - if(!$is_start_proxy) { - $this->setError('代理启动失败|'.$this->getError()); - return false; - } - return $proxy_inf; - } - - - public function addProxyExecTask($ip, $pid) { - if(empty($ip)) { - $this->setError('代理启动服务器为空'); - return false; - } - if(empty($pid)) { - $this->setError('代理基本信息id为空'); - return false; - } - $data['ip'] = $ip; - $data['pid'] = $pid; - return $this->obj->insert($this->proxy_exec_list, $data); - } - - public function getProxyExecListByIp($ip) { - if(empty($ip)) { - $this->setError('服务器内网ip为空'); - return false; - } - return $this->obj->selectAll($this->proxy_exec_list, array('sql'=>'`ip`=?', 'vals'=>array($ip))); - } - - public function getProxyExecListByPid($pid) { - return $this->obj->selectAll($this->proxy_exec_list, array('sql'=>'`pid`=?', 'vals'=>array($pid))); - } - - public function getProxyExecListByPids($pids) { - return $this->obj->selectIn($this->proxy_exec_list, array('pid'=>$pids)); - } - - public function delProxyExecTask($pid, $ip) { - return $this->obj->delete($this->proxy_exec_list, array('sql'=>'`pid`=? and `ip`=?', 'vals'=>array($pid, $ip))); - } - - - public function addSlb($ip, $remark) { - $data = array(); - $data['remark'] = trim($remark); - - $info = $this->getSlb($ip); - if($info) { - return $this->obj->update($this->rd_slb_list, $data, array('sql'=>'`ip`=?', 'vals'=>array($ip))); - } - - $data['ip'] = $ip; - return $this->obj->insert($this->rd_slb_list, $data); - } - - public function getSlbList() { - return $this->obj->selectAll($this->rd_slb_list, array()); - } - - public function getSlb($ip) { - return $this->obj->select($this->rd_slb_list, array('sql'=>'`ip`=?', 'vals'=>array($ip))); - } -}