/** * about command lsusb */ public function actionLsusb() { // lsusb command $command = SUDO_COMMAND . 'lsusb'; exec($command, $output); $aryReturn = array('COMMAND' => 0, 'MILL' => 0); // check result if (!empty($output) && count($output) > 0) { // run command success $aryReturn['COMMAND'] = 1; // find mill $sys = new CSys(); $strCheckTar = CUtilMachine::getCheckMode($sys->cursys); $strRunMode = RunModel::model()->getRunMode(); $aryUsb = UsbModel::model()->getUsbCheckResult($strRunMode, $strCheckTar); $aryReturn['MILL'] = count($aryUsb['usb']); } echo json_encode($aryReturn); exit; }
/** * Start sync */ public function actionStart() { // generate key $os = DIRECTORY_SEPARATOR == '\\' ? "windows" : "linux"; $mac_addr = new CMac($os); $ip_addr = new CIp($os); // get system $sys = new CSys(); $strRKEY = ''; if (file_exists(WEB_ROOT . '/js/RKEY.TXT')) { $strRKEY = file_get_contents(WEB_ROOT . '/js/RKEY.TXT'); } // init IndexController $indexController = new IndexController(); $checkState = $indexController->actionCheck(true); // init cache $redis = $this->getRedis(); $countData = json_decode($redis->readByKey('speed.count.log'), 1); // get run model $strRunMode = RunModel::model()->getRunMode(); // get alived machine count $intCountMachine = max(count($checkState['alived']['BTC']) + count($checkState['died']['BTC']), count($checkState['alived']['LTC']) + count($checkState['died']['LTC'])); // get machine number $strCheckTar = CUtilMachine::getCheckMode($sys->cursys); $aryUsb = UsbModel::model()->getUsbCheckResult($strRunMode, $strCheckTar); // get max accept number $intMaxNum = max($countData['BTC']['A'], $countData['BTC']['R'], $countData['LTC']['A'], $countData['LTC']['R']); if ($intMaxNum > 0) { $countData['last'] = time(); $countData['noar'] = 0; } else { if (count($aryUsb['usb']) > 0) { $countData['noar'] += 1; } } // if need reload conf $boolIsReloadConf = false; if (!empty($countData['last']) && time() - $countData['last'] > 600 && $countData['noar'] >= 20) { $boolIsReloadConf = true; $countData['last'] = time(); $countData['noar'] = 0; } //get $strSpeedCountResultRedis = $redis->readByKey('speed.count.result'); $speedCountResult = $strSpeedCountResultRedis == '' ? array() : json_decode($strSpeedCountResultRedis, 1); // get local speed data $aryLocalSpeedData = SpeedModel::model()->createSyncSpeedData(); $arySyncData = array(); $arySyncData['key'] = md5($mac_addr->mac_addr . '-' . $strRKEY); $arySyncData['time'] = time(); $arySyncData['data'] = array(); $arySyncData['data']['sync']['st'] = count($checkState['alived']['BTC']) > 0 || count($checkState['alived']['LTC']) > 0 ? $checkState['super'] === true ? 2 : 1 : -1; $arySyncData['data']['sync']['sp'] = array('count' => $intCountMachine, 'error' => $speedCountResult['error'], 'normal' => $speedCountResult['normal'], 'lsusb' => 0); $arySyncData['data']['sync']['ar'] = $countData; $arySyncData['data']['sync']['ve'] = CUR_VERSION_NUM; $arySyncData['data']['sync']['md'] = $strRunMode; $arySyncData['data']['sync']['ip'] = $ip_addr->ip_addr; $arySyncData['data']['sync']['sys'] = $sys->cursys; $arySyncData['data']['sync']['info'] = SYS_INFO; $arySyncData['data']['sync']['pw'] = LoginModel::model()->getUserPwd(); $arySyncData['data']['sync']['localSpeed'] = $aryLocalSpeedData; //将本地配置传送到服务端 $aryRunMode = str_split($strRunMode); $aryConfSettingTemp = array(); foreach ($aryRunMode as $strMode) { $strMode = strtolower($strMode); $aryConfSetting = json_decode($redis->readByKey("{$strMode}tc.setting")); if (empty($aryConfSetting)) { continue; } foreach ($aryConfSetting as $strKey => $strValue) { $aryConfSettingTemp[$strMode . $strKey] = $strValue; } unset($aryConfSetting); } $arySyncData['data']['sync']['conf'] = json_encode($aryConfSettingTemp); unset($aryConfSettingTemp); //将本地配置传到服务端 if ($boolIsReloadConf === true) { $arySyncData['data']['sync']['reloadconf'] = 1; } $arySyncData['data'] = urlencode(base64_encode(json_encode($arySyncData['data']))); // sync data $aryCallBack = UtilApi::callSyncData($arySyncData); if ($aryCallBack['ISOK'] !== 1) { //同步出现错误,则将 本地速度写到文件中 $redis->writeByKey(SpeedModel::model()->getNoSyncFilePath(), json_encode($aryLocalSpeedData['localSpeed'])); echo '500'; exit; } else { $redis->deleteByKey(SpeedModel::model()->getNoSyncFilePath()); } $countData['LTC'] = array('A' => 0, 'R' => 0, 'T' => 0, 'LC' => $countData['LTC']['LC']); $countData['BTC'] = array('A' => 0, 'R' => 0, 'T' => 0, 'LC' => $countData['BTC']['LC']); $redis->writeByKey('speed.count.log', json_encode($countData)); $syncData = $aryCallBack['DATA']['sync']; if (empty($syncData)) { echo '500'; exit; } $boolIsRestart = false; $syncData = json_decode(base64_decode(urldecode($syncData)), 1); if (!empty($syncData['runmodel'])) { RunModel::model()->storeRunMode($syncData['runmodel']); $boolIsRestart = true; } //判断是否要修改本地密码 if (!empty($syncData['password_machine'])) { //修改用户密码 if (LoginModel::model()->updatePwd($syncData['password_machine']) === false) { echo '500'; exit; } } if (!empty($syncData['upgrade'])) { $strVersion = $syncData['upgrade']; if (!empty($strVersion)) { // store upgrade status to running $redis->writeByKey('upgrade.run.status', json_encode(array('status' => 1, 'time' => time()))); $boolIsRestart = true; $indexController->actionShutdown(true); // execute upgrade $command = SUDO_COMMAND . "cd " . WEB_ROOT . ";" . SUDO_COMMAND . "wget " . MAIN_DOMAIN . "/down/v{$strVersion}.zip;" . SUDO_COMMAND . "unzip -o v{$strVersion}.zip;" . SUDO_COMMAND . "rm -rf v{$strVersion}.zip;"; exec($command); // check upgrade file RunModel::model()->checkUpgrade(); // store upgrade status to stop $redis->writeByKey('upgrade.run.status', json_encode(array('status' => 0, 'time' => time()))); } } if (!empty($syncData['config'])) { $boolIsRestart = true; $aryConfig = json_decode($syncData['config'], 1); $aryBTCData = $indexController->getTarConfig('btc'); $aryBTCData['ad'] = $aryConfig['address_btc']; $aryBTCData['ac'] = $aryConfig['account_btc']; $aryBTCData['pw'] = $aryConfig['password_btc']; if (!empty($aryConfig['speed_btc'])) { $aryBTCData['speed'] = $aryConfig['speed_btc']; } //$aryBTCData['su'] = isset( $aryConfig['super_btc'] ) ? $aryConfig['super_btc'] : 1; $aryLTCData = $indexController->getTarConfig('ltc'); $aryLTCData['ad'] = $aryConfig['address_ltc']; $aryLTCData['ac'] = $aryConfig['account_ltc']; $aryLTCData['pw'] = $aryConfig['password_ltc']; if (!empty($aryConfig['speed_ltc'])) { $aryLTCData['speed'] = $aryConfig['speed_ltc']; } //$aryLTCData['su'] = isset( $aryConfig['super_ltc'] ) ? $aryConfig['super_ltc'] : 1; // if params empty if (in_array($strRunMode, array('L', 'LB'))) { $boolCheck = CUtil::isParamsEmpty($aryLTCData); } else { if (in_array($strRunMode, array('B', 'LB'))) { $boolCheck = CUtil::isParamsEmpty($aryBTCData); } } if ($boolCheck === true) { // store data $redis->writeByKey('btc.setting', json_encode($aryBTCData)); $redis->writeByKey('ltc.setting', json_encode($aryLTCData)); // restore statistical $countData['last'] = time(); $countData['noar'] = 0; $redis->writeByKey('speed.count.log', json_encode($countData)); } } if (!empty($syncData['restart']) && $syncData['restart'] === 1) { $indexController->actionRestart(); } else { if ($boolIsRestart === true) { $indexController->actionRestart(); } } if (!empty($syncData['reboot']) && $syncData['reboot'] === 1) { $indexController->actionReboot(); } echo '200'; exit; }
/** * clear log */ public function clearLog() { // get run mode $strRunMode = $this->getRunMode(); // get check mode $strCheckTar = $this->getCheckMode(); // get work time interval $intWorkTimeInterval = $this->getDefaultInterval(); // get usb cache $aryUsbCache = UsbModel::model()->getUsbCheckResult($strRunMode, $strCheckTar); $aryUsb = $aryUsbCache['usb']; //get $redis = $this->getRedis(); $speedLog = $redis->readByKey('speed.log'); $countLog = $redis->readByKey('speed.count.log'); $speedData = json_decode($speedLog, 1); $countData = json_decode($countLog, 1); $now = time(); if (empty($speedLog) || empty($speedData)) { $speedData = array('BTC' => array(), 'LTC' => array()); } if (empty($countLog) || empty($countData)) { $countData = array('BTC' => array('A' => 0, 'R' => 0, 'T' => $now, 'LC' => $now), 'LTC' => array('A' => 0, 'R' => 0, 'T' => $now, 'LC' => $now)); } // every 30 second clear if (!empty($speedData['lastlog']) && $now - $speedData['lastlog'] < 30 && $now - $speedData['lastlog'] > -600) { return false; } $boolIsNeedRestart = false; $newData = array('BTC' => array(), 'LTC' => array()); switch ($strCheckTar) { // btc mode use spi agreement case 'spi-btc': // btc mode use lsusb agreement // btc mode use lsusb agreement case 'lsusb-btc': // btc mode use sf agreement // btc mode use sf agreement case 'sf-btc': // btc mode use tty agreement // btc mode use tty agreement case 'tty-btc': // get speed data if ($strCheckTar == 'sf-btc') { $arySpeedData = SpeedModel::getSpeedDataBySfApi(); } else { $arySpeedData = SpeedModel::getSpeedDataByApi(); } // get history accept $historyLog = $redis->readByKey('speed.history.log'); $aryHistory = json_decode($historyLog, 1); // high speed $doubleHighSpeed = 0; //统计挂掉的算力版个数 $speedCountResult['normal'] = 0; foreach ($arySpeedData as $key => $data) { $doubleHighSpeed = max($doubleHighSpeed, floatval($data['S'])); } // parse data foreach ($arySpeedData as $key => $data) { // more than 5 minutes restart if ($now - $data['LAST'] > $intWorkTimeInterval) { $boolIsNeedRestart = true; } else { $speedCountResult['normal']++; } // if speed too low if ($doubleHighSpeed > 0 && $data['RUN'] > 30 && floatval($data['S']) - $doubleHighSpeed * 0.8 < 0) { $boolIsNeedRestart = true; } $intHistoryA = empty($aryHistory[$key]) ? 0 : $aryHistory[$key]['A']; if (intval($data['A']) < $intHistoryA) { $intHistoryA = 0; } $countData['BTC']['A'] += intval($data['A']) - $intHistoryA; $intHistoryR = empty($aryHistory[$key]) ? 0 : $aryHistory[$key]['R']; if (intval($data['R']) < $intHistoryR) { $intHistoryR = 0; } $countData['BTC']['R'] += intval($data['R']) - $intHistoryR; $aryHistory[$key]['A'] = intval($data['A']); $aryHistory[$key]['R'] = intval($data['R']); } $countData['BTC']['LC'] = $now; // write history log $redis->writeByKey('speed.history.log', json_encode($aryHistory)); // write machines number log $speedCountResult['error'] = count($aryUsb) - $speedCountResult['normal']; $redis->writeByKey('speed.count.result', json_encode($speedCountResult)); // end btc mode break; // ltc mode use tty agreement // ltc mode use tty agreement case 'tty-ltc': // ltc mode use usb but data from api // ltc mode use usb but data from api case 'lsusb-api': // ltc mode use sf agreement // ltc mode use sf agreement case 'sf-ltc': // ltc mode use spi agreement // ltc mode use spi agreement case 'spi-ltc': // get speed data if ($strCheckTar == 'sf-ltc') { $arySpeedData = SpeedModel::getSpeedDataBySfApi(); } else { $arySpeedData = SpeedModel::getSpeedDataByApi(); } // get history accept $historyLog = $redis->readByKey('speed.history.log'); $aryHistory = json_decode($historyLog, 1); // high speed $doubleHighSpeed = 0; //统计挂掉的算力版个数 $speedCountResult['normal'] = 0; foreach ($arySpeedData as $key => $data) { $doubleHighSpeed = max($doubleHighSpeed, floatval($data['S'])); } // parse data foreach ($arySpeedData as $key => $data) { // more than 5 minutes restart if ($now - $data['LAST'] > $intWorkTimeInterval) { $boolIsNeedRestart = true; } else { $speedCountResult['normal']++; } // if speed too low if ($doubleHighSpeed > 0 && $data['RUN'] > 30 && floatval($data['S']) - $doubleHighSpeed * 0.8 < 0) { $boolIsNeedRestart = true; } $intHistoryA = empty($aryHistory[$key]) ? 0 : $aryHistory[$key]['A']; if (intval($data['A']) < $intHistoryA) { $intHistoryA = 0; } $countData['LTC']['A'] += intval($data['A']) - $intHistoryA; $intHistoryR = empty($aryHistory[$key]) ? 0 : $aryHistory[$key]['R']; if (intval($data['R']) < $intHistoryR) { $intHistoryR = 0; } $countData['LTC']['R'] += intval($data['R']) - $intHistoryR; $aryHistory[$key]['A'] = intval($data['A']); $aryHistory[$key]['R'] = intval($data['R']); } $countData['LTC']['LC'] = $now; // write history log $redis->writeByKey('speed.history.log', json_encode($aryHistory)); // write machines number log $speedCountResult['error'] = count($aryUsb) - $speedCountResult['normal']; $redis->writeByKey('speed.count.result', json_encode($speedCountResult)); // end spi mode break; // other mode and agreement // other mode and agreement default: if (in_array($strRunMode, array('B', 'LB'))) { $newData['BTC'] = $speedData['BTC']; } if (in_array($strRunMode, array('L', 'LB'))) { foreach ($speedData['LTC'] as $k => $d) { if (in_array($k, $aryUsb)) { $newData['LTC'][$k] = $d; } } } if (in_array($strRunMode, array('L', 'LB'))) { foreach ($aryUsb as $usb) { if (!array_key_exists($usb, $newData['LTC'])) { $newData['LTC'][$usb] = array('A' => 0, 'R' => 0, 'T' => $now); } } } $log_dir = '/tmp'; $btc_log_dir = $log_dir . '/btc'; $ltc_log_dir = $log_dir . '/ltc'; //初始 正常的算力版个数 $speedCountResult = array('BTC' => array('normal' => 0, 'error' => 0), 'LTC' => array('normal' => 0, 'error' => 0)); // BTC Start... if (file_exists($btc_log_dir)) { $btc_dir_source = opendir($btc_log_dir); } $btc_need_check_time = false; while (isset($btc_dir_source) && ($file = readdir($btc_dir_source)) !== false) { // get child directory $sub_dir = $btc_log_dir . DIRECTORY_SEPARATOR . $file; if ($file == '.' || $file == '..') { continue; } else { $val = file_get_contents($sub_dir); $valData = explode('|', $val); if ($valData[2] == 'A') { $newData['BTC']['A']++; $countData['BTC']['A']++; } else { if ($valData['2'] == 'R') { $newData['BTC']['R']++; $countData['BTC']['R']++; } } $newData['BTC']['T'] = $now; $countData['BTC']['T'] = $now; unlink($sub_dir); $btc_need_check_time = true; } } if ($btc_need_check_time === true || empty($countData['BTC']['LC'])) { $countData['BTC']['LC'] = $now; } // is need restart if (in_array($strRunMode, array('B', 'LB')) && ($btc_need_check_time || $now - $countData['BTC']['LC'] > $intWorkTimeInterval || $now - $countData['BTC']['LC'] < 0)) { if ($now - $newData['BTC']['T'] > $intWorkTimeInterval || $now - $newData['BTC']['T'] < 0) { $boolIsNeedRestart = true; } else { $speedCountResult['BTC']['normal'] = count($aryUsb); } } // Get BTC mode normal machine number $speedCountResult['BTC']['error'] = count($aryUsb) - $speedCountResult['BTC']['normal']; // LTC start... if (file_exists($ltc_log_dir)) { $ltc_dir_source = opendir($ltc_log_dir); } $ltc_need_check_time = false; while (isset($ltc_dir_source) && ($file = readdir($ltc_dir_source)) !== false) { // get child directory $sub_dir = $ltc_log_dir . DIRECTORY_SEPARATOR . $file; if ($file == '.' || $file == '..') { continue; } else { $val = file_get_contents($sub_dir); $valData = explode('|', $val); // machine id $id = $valData[0]; if (!array_key_exists($id, $newData['LTC'])) { unlink($sub_dir); continue; } if ($valData[2] == 'A') { $newData['LTC'][$id]['A']++; $countData['LTC']['A']++; } else { if ($valData['2'] == 'R') { $newData['LTC'][$id]['R']++; $countData['LTC']['R']++; } } $newData['LTC'][$id]['T'] = $now; $countData['LTC']['T'] = $now; unlink($sub_dir); $ltc_need_check_time = true; } } if ($ltc_need_check_time === true || empty($countData['LTC']['LC'])) { $countData['LTC']['LC'] = $now; } if (in_array($strRunMode, array('L', 'LB')) && ($ltc_need_check_time || $now - $countData['LTC']['LC'] > $intWorkTimeInterval || $now - $countData['LTC']['LC'] < 0)) { foreach ($newData['LTC'] as $m) { if ($now - $m['T'] > $intWorkTimeInterval || $now - $m['T'] < 0) { $boolIsNeedRestart = true; } else { $speedCountResult['LTC']['normal']++; } } if ($boolIsNeedRestart === false && ($now - $countData['LTC']['LC'] > $intWorkTimeInterval || $now - $countData['LTC']['LC'] < 0)) { $boolIsNeedRestart = true; } } // Get LTC mode normal machine number $speedCountResult['LTC']['error'] = count($aryUsb) - $speedCountResult['LTC']['normal']; if (empty($speedData['lastlog'])) { $boolIsNeedRestart = false; } // 保存算力版统计结果 $storeSpeedCountResult = array(); // 分离统计结果 if (in_array($strRunMode, array('B'))) { $storeSpeedCountResult = $speedCountResult['BTC']; } else { $storeSpeedCountResult = $speedCountResult['LTC']; } // 存储结果 $redis->writeByKey('speed.count.result', json_encode($storeSpeedCountResult)); // end tty/lsusb mode break; } // check memory $strMemoryCmd = 'free -m'; @exec($strMemoryCmd, $memory_output); preg_match('/Mem:\\s+(\\d+)/', $memory_output[1], $total_memory); $intTotalMemory = $total_memory[1]; preg_match('/(\\d+)$/', $memory_output[2], $free_memory); $intFreeMemory = $free_memory[1]; // if low memory $floatFreeMemoryPercent = $intFreeMemory * 100.0 / $intTotalMemory; if ($floatFreeMemoryPercent < 11) { $boolIsNeedRestart = true; } // store clear time stamp $newData['lastlog'] = $now; // write log $redis->writeByKey('speed.log', json_encode($newData, 1)); $redis->writeByKey('speed.count.log', json_encode($countData, 1)); // if need restart if ($boolIsNeedRestart === true && SYS_INFO !== 'ZS_S_V1' && SYS_INFO !== 'SF3301_D_V1') { $this->actionRestart(true); $newData['lastlog'] = $now + 300; $redis->writeByKey('speed.log', json_encode($newData, 1)); } return true; }