Esempio n. 1
0
 /**
  * Returns array of serial ports. Can return fake list for testing purposes.
  * 
  * @return array Array of serial ports.
  */
 public static function getAvailableComPortsList()
 {
     if (Yii::app()->params['show_fake_com_ports']) {
         return array('COM1' => 'COM1', 'COM2' => 'COM2');
     }
     $output = null;
     $result = array();
     if (It::isLinux()) {
         exec('setserial -g /dev/ttyS*', $output);
         if (is_array($output)) {
             foreach ($output as $line) {
                 $matches = array();
                 if (preg_match('/\\/dev\\/ttyS([0-9])/', $line, $matches)) {
                     $serialPort = 'COM' . ($matches[1] + 1);
                     $result[$serialPort] = $matches[0];
                 }
             }
         }
         $result = array_unique($result);
     } else {
         if (it::isWindows()) {
             exec('wmic path Win32_SerialPort get Description, DeviceID /format:csv', $output);
             if (is_array($output) && count($output) > 1) {
                 $output = array_slice($output, 2);
                 foreach ($output as $line) {
                     $values = explode(',', $line);
                     $result[$values[2]] = $values[1];
                 }
             }
         }
     }
     return $result;
 }
Esempio n. 2
0
 public function test_SerialPortList_Linux()
 {
     $this->assertTrue(It::isLinux());
     Yii::app()->params['show_fake_com_ports'] = false;
     $result = SysFunc::getAvailableComPortsList();
     $expected = array('COM1' => 'COM1 (/dev/ttyS0)');
     $this->assertEquals($expected, $result);
 }
Esempio n. 3
0
 /**
  * Kills process with PID = $pid
  *  
  * @param int $pid
  * @return boolean 
  */
 public static function killProcess($pid)
 {
     if (It::isLinux()) {
         exec('kill -s KILL ' . $pid);
     } else {
         if (It::isWindows()) {
             exec("taskkill /pid " . $pid . " /f");
         }
     }
 }
Esempio n. 4
0
 public function rules()
 {
     $res = array(array('overwrite_data_on_import,overwrite_data_on_listening', 'boolean', 'falseValue' => 0, 'trueValue' => 1, 'on' => 'other'), array('current_company_name', 'required', 'on' => 'other'), array('current_company_name', 'length', 'max' => 50, 'allowEmpty' => false, 'on' => 'other'), array('xml_check_frequency', 'numerical', 'integerOnly' => true, 'on' => 'other'), array('local_timezone_id', 'length', 'allowEmpty' => false, 'on' => 'other'), array('mail__use_fake_sendmail', 'boolean', 'allowEmpty' => false, 'trueValue' => 1, 'falseValue' => 0, 'on' => 'mail'), array('mail__sender_address', 'email', 'allowEmpty' => false, 'on' => 'mail'), array('mail__sender_address,mail__sender_name,mail__sender_password,mail__smtp_server', 'length', 'max' => 255, 'allowEmpty' => false, 'on' => 'mail'), array('mail__smtp_port', 'numerical', 'integerOnly' => true, 'on' => 'mail'), array('mail__smtps_support', 'in', 'range' => array('auto', 'ssl', 'tls', 'none'), 'allowEmpty' => false, 'on' => 'mail'), array('db_exp_enabled', 'boolean', 'allowEmpty' => false, 'trueValue' => 1, 'falseValue' => 0, 'on' => 'dbexport'), array('db_exp_period', 'numerical', 'integerOnly' => true, 'on' => 'dbexport'), array('db_exp_frequency', 'numerical', 'integerOnly' => true, 'on' => 'dbexport'), array('db_exp_sql_host', 'checkHost', 'on' => 'dbexport'), array('db_exp_sql_host', 'checkHostExists', 'on' => 'dbexport'), array('db_exp_sql_port', 'numerical', 'integerOnly' => true, 'allowEmpty' => false, 'min' => 1, 'on' => 'dbexport'), array('db_exp_sql_dbname', 'match', 'pattern' => '/^[A-Z,a-z,0-9,_,-]{0,30}$/', 'on' => 'dbexport'), array('db_exp_sql_dbname,db_exp_sql_login', 'required', 'on' => 'dbexport'), array('db_exp_sql_dbname,db_exp_sql_login', 'length', 'allowEmpty' => false, 'max' => 255, 'on' => 'dbexport'), array('db_exp_sql_password', 'checkUser', 'on' => 'dbexport'), array('scheduled_reports_path', 'safe', 'on' => 'other'));
     if (It::isLinux()) {
         $pattern = '/^[\\/]([A-Za-z0-9-_\\s\\/\\.]){1,251}$/';
         //$res[] = array('scheduled_reports_path, xml_messages_path', 'match', 'pattern' => $pattern, 'on' => 'other');
     } elseif (It::isWindows()) {
         $pattern = '/^([A-Z]{1})(:\\\\)([A-Za-z0-9-_\\s\\.]+[\\\\]?){1,251}$/';
         $res[] = array('scheduled_reports_path, xml_messages_path', 'match', 'pattern' => $pattern, 'on' => 'other');
     }
     return $res;
 }
Esempio n. 5
0
 public function createSync()
 {
     $applicationsPaths = $this->getConfigFile('application_params');
     if (TaskManager::check($this->sync_id) === false) {
         $command = $applicationsPaths['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php syncdb";
         TaskManager::create($this->sync_id, $command, $this->sync_periodicity, $this->sync_interval, $this->sync_startTime);
     }
     if (TaskManager::check($this->db_backup_id) === false) {
         $backup_path = Yii::app()->params['backups_path'] . DIRECTORY_SEPARATOR . (It::isLinux() ? '`echo "$""(date +\\%a)"`' : '%DATE:~0,3%') . '_long.sql';
         // Schedule daily database backup
         //                $command = $install->getConfigSection('path','mysqldump_exe_path') .
         $command = $applicationsPaths['mysqldump_exe_path'] . ' --user="******"' . ' --password="******"' . ' --result-file="' . $backup_path . '" ' . $this->dbname;
         TaskManager::create($this->db_backup_id, $command, 'daily', 1, '4:00');
     }
     if (TaskManager::check($this->heartbeat_id) === false) {
         $command = $applicationsPaths['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php heartbeatreport";
         TaskManager::create($this->heartbeat_id, $command, 'minutely', 10);
     }
 }
Esempio n. 6
0
 /**
  * Deletes task by name.
  *
  * @param string $name
  */
 public static function delete($name)
 {
     if (It::isLinux()) {
         exec('crontab -l | grep -v ' . $name . ' | crontab -');
     } else {
         if (It::isWindows()) {
             exec('schtasks /delete /tn ' . $name . ' /f');
         }
     }
 }
Esempio n. 7
0
 public static function runAsynchCommand($command)
 {
     if (It::isLinux()) {
         $output = null;
         $return = null;
         exec('nohup ' . $command . ' > /dev/null 2> /dev/null &', $output, $return);
     } else {
         if (It::isWindows()) {
             @pclose(@popen('start /B ' . $command, 'r'));
         }
     }
 }
Esempio n. 8
0
 public function actionDbsetup()
 {
     // #############################################################################
     $backups_path = Yii::app()->params['backups_path'];
     if (isset($_REQUEST['apply'])) {
         $apply = trim($_REQUEST['apply']);
         if (file_exists($backups_path . DIRECTORY_SEPARATOR . $apply)) {
             ini_set('memory_limit', '-1');
             set_time_limit(0);
             $sql = file_get_contents($backups_path . DIRECTORY_SEPARATOR . $apply);
             //use long or short db
             if (stripos($apply, 'long')) {
                 $res = Yii::app()->db_long->createCommand($sql)->query();
             } else {
                 $res = Yii::app()->db->createCommand($sql)->query();
             }
             It::memStatus('admin_backup_applied');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     if (isset($_REQUEST['delete'])) {
         $delete = trim($_REQUEST['delete']);
         if (file_exists($backups_path . DIRECTORY_SEPARATOR . $delete)) {
             unlink($backups_path . DIRECTORY_SEPARATOR . $delete);
             It::memStatus('admin_backup_deleted');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     if (isset($_REQUEST['create'])) {
         $backup_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups';
         if (It::isLinux()) {
             $backup_path .= DIRECTORY_SEPARATOR . '`date +%Y_%m_%d`.sql';
         } else {
             if (It::isWindows()) {
                 $backup_path .= DIRECTORY_SEPARATOR . '%DATE:~7,4%_%DATE:~3,2%_%DATE:~0,2%.sql';
             }
         }
         $command = Yii::app()->params['applications']['mysqldump_exe_path'] . ' --user="******"' . ' --password="******"' . ' --result-file="' . $backup_path . '" ' . Yii::app()->params['db_params']['dbname'];
         if (DOCKER) {
             $backup_name = '`date +%Y_%m_%d`.sql';
             $command = 'echo ' . $backup_name . ' >' . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups' . DIRECTORY_SEPARATOR . 'new';
         }
         set_time_limit(1800);
         // 30min
         $output = null;
         $return = null;
         exec($command, $output, $return);
         if ($return == 0) {
             It::memStatus('admin_backup_refreshed');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     if (isset($_REQUEST['create_long'])) {
         $backup_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups';
         if (It::isLinux()) {
             $backup_path .= DIRECTORY_SEPARATOR . '`date +%Y_%m_%d`_long.sql';
         } else {
             if (It::isWindows()) {
                 $backup_path .= DIRECTORY_SEPARATOR . '%DATE:~7,4%_%DATE:~3,2%_%DATE:~0,2%_long.sql';
             }
         }
         $command = Yii::app()->params['applications']['mysqldump_exe_path'] . ' --user="******"' . ' --password="******"' . ' --result-file="' . $backup_path . '" ' . Yii::app()->params['db_long_params']['dbname'];
         if (DOCKER) {
             $backup_name = '`date +%Y_%m_%d`_long.sql';
             $command = 'echo ' . $backup_name . ' >' . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups' . DIRECTORY_SEPARATOR . 'new_long';
         }
         set_time_limit(1800);
         // 30min
         $output = null;
         $return = null;
         exec($command, $output, $return);
         if ($return == 0) {
             It::memStatus('admin_backup_refreshed');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     $outputs = null;
     if (It::isLinux()) {
         $cmd = 'ls -1At ' . $backups_path . ' | egrep -i *.sql';
         exec($cmd, $outputs);
     } else {
         if (It::isWindows()) {
             exec('dir ' . $backups_path . DIRECTORY_SEPARATOR . '*.sql /B /4 /T:C /O:D', $outputs);
             array_slice($outputs, 0, -1);
         }
     }
     $backups = array();
     foreach ($outputs as $output) {
         $backups[] = array('filename' => $output, 'created' => filemtime($backups_path . DIRECTORY_SEPARATOR . $output));
     }
     // #############################################################################
     $settings = Settings::model()->find();
     if (is_null($settings)) {
         $settings = new Settings();
     }
     $settings->scenario = 'dbexport';
     if (Yii::app()->request->isPostRequest && isset($_POST['Settings'])) {
         $settings->attributes = $_POST['Settings'];
         if (!$settings->attributes['db_exp_enabled']) {
             Settings::model()->updateByPk(1, array('db_exp_enabled' => 0));
             It::memStatus('admin_dbexport_settings_saved');
             $this->redirect($this->createUrl('admin/dbsetup'));
         } else {
             if ($settings->validate()) {
                 if ($settings->save(false)) {
                     It::memStatus('admin_dbexport_settings_saved');
                     $this->redirect($this->createUrl('admin/dbsetup'));
                 }
             }
         }
     }
     // #############################################################################
     $res = BackupOldDataTxLog::model()->prepareList();
     // ###########################################################################
     $this->render('dbsetup', array('backups' => $backups, 'settings' => $settings, 'list' => $res['list'], 'pages' => $res['pages']));
 }
Esempio n. 9
0
 public function test_IsLinux()
 {
     $result = It::isLinux();
     $expected = substr(strtolower(php_uname('s')), 0, 5) === 'linux';
     $this->assertEquals($expected, $result);
 }
Esempio n. 10
0
 /**
  * Calculated data
  */
 private function calculateData()
 {
     $databases = array('db' => false, 'db_long' => true);
     /**
      * for last backups
      */
     $backups_path = Yii::app()->params['backups_path'];
     $outputs = null;
     if (It::isLinux()) {
         $cmd = 'ls -1At ' . $backups_path . ' | egrep -i *.sql';
         exec($cmd, $outputs);
     } else {
         if (It::isWindows()) {
             exec('dir ' . $backups_path . DIRECTORY_SEPARATOR . '*.sql /B /4 /T:C /O:D', $outputs);
             array_slice($outputs, 0, -1);
         }
     }
     /*
      * $db_small_stat
      * $db_long_small_stat
      */
     foreach ($databases as $database => $var) {
         $this->{$database . '_small_stat'} = array('Uptime' => number_format($this->{$database . '_stat'}['Uptime'] / 60 / 60) . 'h' . $this->{$database . '_stat'}['Uptime'] % 60 . 'm', 'Queries' => $this->{$database . '_stat'}['Queries']);
         foreach ($outputs as $output) {
             if (strpos($output, '_long.sql')) {
                 if ($var) {
                     $this->{$database . '_small_stat'}['Last_backup'] = date("y-m-d H:i", filemtime($backups_path . DIRECTORY_SEPARATOR . $output));
                     break;
                 }
             } else {
                 if (!$var) {
                     $this->{$database . '_small_stat'}['Last_backup'] = date("y-m-d H:i", filemtime($backups_path . DIRECTORY_SEPARATOR . $output));
                     break;
                 }
             }
         }
         $this->{$database . '_small_stat'} += array('Size' => $this->{$database . '_tables_size'}['All'] . 'Mb', 'Data_read' => number_format($this->{$database . '_stat'}['Innodb_data_read'] / 1024) . 'Mb', 'Data_writes' => number_format($this->{$database . '_stat'}['Innodb_data_writes'] / 1024) . 'Mb', 'Rows' => $this->{$database . '_tables_rows'}['All'], 'Rows_deleted' => $this->{$database . '_stat'}['Innodb_rows_deleted'], 'Rows_inserted' => $this->{$database . '_stat'}['Innodb_rows_inserted'], 'Rows_read' => $this->{$database . '_stat'}['Innodb_rows_read'], 'Rows_updated' => $this->{$database . '_stat'}['Innodb_rows_updated'], 'Connections' => $this->{$database . '_stat'}['Connections']);
     }
 }
Esempio n. 11
0
 public function setSchedule()
 {
     $this->unsetScheduleCommands();
     if ($this->checkTask($this->db_backup_id) === false) {
         $backup_path = Yii::app()->params['backups_path'] . DIRECTORY_SEPARATOR . (It::isLinux() ? '`echo "$""(date +\\%a)"`' : '%DATE:~0,3%') . '.sql';
         // Schedule daily database backup
         $command = $this->mysqldump_exe_path . ' --user="******"' . ' --password="******"' . ' --result-file="' . $backup_path . '" ' . $this->dbname;
         $this->createTask($this->db_backup_id, $command, 'daily', 1, '2:00');
     }
     if ($this->checkTask($this->each_minute_process_id) === false) {
         // Schedule report generation
         $command = Yii::app()->params['applications']['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php schedule";
         $this->createTask($this->each_minute_process_id, $command, 'minutely');
         //				$this->createTask($this->each_minute_process_id, $command, 'minutely', 15, '00:02');
     }
     if ($this->checkTask($this->get_xml_process_id) === false) {
         // Schedule XML messages grabbing
         $command = Yii::app()->params['applications']['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php getxml";
         $this->createTask($this->get_xml_process_id, $command, 'minutely');
     }
     if ($this->checkTask($this->check_processes_process_id) === false) {
         // Schedule check listening process
         $command = Yii::app()->params['applications']['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php checkprocesses";
         $this->createTask($this->check_processes_process_id, $command, 'minutely');
     }
     if ($this->checkTask($this->each_minute_prepare_process_id) === false) {
         // Schedule prepare message script
         $command = Yii::app()->params['applications']['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php prepare";
         $this->createTask($this->each_minute_prepare_process_id, $command, 'minutely');
     }
     if ($this->checkTask($this->backup_process_id) === false) {
         // Schedule backup old data
         $command = Yii::app()->params['applications']['php_exe_path'] . " -f  " . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "console.php backupOldData";
         $this->createTask($this->backup_process_id, $command, 'monthly');
     }
     $this->saveScheduleConfig();
 }