| Conditions | 17 |
| Paths | 921 |
| Total Lines | 145 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 71 | public function mibAction() |
||
| 72 | { |
||
| 73 | $this->prepareTabs()->activate('mib'); |
||
| 74 | |||
| 75 | $this->view->uploadStatus=null; |
||
| 76 | // check if it is an ajax query |
||
| 77 | if ($this->getRequest()->isPost()) |
||
| 78 | { |
||
| 79 | $postData=$this->getRequest()->getPost(); |
||
| 80 | /** Check for action update or check update */ |
||
| 81 | if (isset($postData['action'])) |
||
| 82 | { |
||
| 83 | $action=$postData['action']; |
||
| 84 | if ($action == 'update_mib_db') |
||
| 85 | { // Do the update in background |
||
| 86 | $return=exec('icingacli trapdirector mib update --pid /tmp/trapdirector_update.pid'); |
||
| 87 | if (preg_match('/OK/',$return)) |
||
| 88 | { |
||
| 89 | $this->_helper->json(array('status'=>'OK')); |
||
| 90 | } |
||
| 91 | // Error |
||
| 92 | $this->_helper->json(array('status'=>$return)); |
||
| 93 | } |
||
| 94 | if ($action == 'check_update') |
||
| 95 | { |
||
| 96 | $file=@fopen('/tmp/trapdirector_update.pid','r'); |
||
| 97 | if ($file == false) |
||
| 98 | { // process is dead |
||
| 99 | $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | $pid=fgets($file); |
||
| 103 | $output=array(); |
||
| 104 | $retVal=0; |
||
| 105 | exec('ps '.$pid,$output,$retVal); |
||
| 106 | if ($retVal == 0) |
||
| 107 | { // process is alive |
||
| 108 | $this->_helper->json(array('status'=>'Alive and kicking')); |
||
| 109 | } |
||
| 110 | else |
||
| 111 | { // process is dead |
||
| 112 | $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible' )); |
||
| 116 | } |
||
| 117 | /** Check for mib file UPLOAD */ |
||
| 118 | if (isset($_FILES['mibfile'])) |
||
| 119 | { |
||
| 120 | $name=$_FILES['mibfile']['name']; |
||
| 121 | $DirConf=explode(':',$this->Config()->get('config', 'snmptranslate_dirs')); |
||
| 122 | $destDir=array_shift($DirConf); |
||
| 123 | if (!is_dir($destDir)) |
||
| 124 | { |
||
| 125 | $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
||
| 126 | } |
||
| 127 | else |
||
| 128 | { |
||
| 129 | if (!is_writable($destDir)) |
||
| 130 | { |
||
| 131 | $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
||
| 132 | } |
||
| 133 | else |
||
| 134 | { |
||
| 135 | $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
||
| 136 | if (move_uploaded_file($_FILES['mibfile']['tmp_name'],$destination)===false) |
||
| 137 | { |
||
| 138 | $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
||
| 139 | } |
||
| 140 | else |
||
| 141 | { |
||
| 142 | $this->view->uploadStatus="File $name uploaded in $destDir"; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | } |
||
| 148 | |||
| 149 | } |
||
| 150 | |||
| 151 | // snmptranslate tests |
||
| 152 | $snmptranslate = $this->Config()->get('config', 'snmptranslate'); |
||
| 153 | $this->view->snmptranslate_bin=$snmptranslate; |
||
| 154 | $this->view->snmptranslate_state='warn'; |
||
| 155 | if (is_executable ( $snmptranslate )) |
||
| 156 | { |
||
| 157 | $translate=exec($snmptranslate . ' 1'); |
||
| 158 | if (preg_match('/iso/',$translate)) |
||
| 159 | { |
||
| 160 | $this->view->snmptranslate='works fine'; |
||
| 161 | $this->view->snmptranslate_state='ok'; |
||
| 162 | } |
||
| 163 | else |
||
| 164 | { |
||
| 165 | $this->view->snmptranslate='can execute but no resolution'; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | else |
||
| 169 | { |
||
| 170 | $this->view->snmptranslate='cannot execute'; |
||
| 171 | } |
||
| 172 | |||
| 173 | // mib database |
||
| 174 | |||
| 175 | $this->view->mibDbCount=$this->getMIB()->countObjects(); |
||
| 176 | $this->view->mibDbCountTrap=$this->getMIB()->countObjects(null,21); |
||
| 177 | |||
| 178 | // mib dirs |
||
| 179 | $DirConf=$this->Config()->get('config', 'snmptranslate_dirs'); |
||
| 180 | $dirArray=explode(':',$DirConf); |
||
| 181 | |||
| 182 | // Get base directories from net-snmp-config |
||
| 183 | $output=$matches=array(); |
||
| 184 | $retVal=0; |
||
| 185 | $sysDirs=exec('net-snmp-config --default-mibdirs',$output,$retVal); |
||
| 186 | if ($retVal==0) |
||
| 187 | { |
||
| 188 | $dirArray=array_merge($dirArray,explode(':',$sysDirs)); |
||
| 189 | } |
||
| 190 | else |
||
| 191 | { |
||
| 192 | $translateOut=exec($this->Config()->get('config', 'snmptranslate') . ' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
||
| 193 | if (preg_match('/MIBDIRS.*\'([^\']+)\'/',$translateOut,$matches)) |
||
| 194 | { |
||
| 195 | $dirArray=array_merge($dirArray,explode(':',$matches[1])); |
||
| 196 | } |
||
| 197 | else |
||
| 198 | { |
||
| 199 | array_push($dirArray,'Install net-snmp-config to see system directories'); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $this->view->dirArray=$dirArray; |
||
| 204 | |||
| 205 | $output=null; |
||
| 206 | foreach (explode(':',$DirConf) as $mibdir) |
||
| 207 | { |
||
| 208 | exec('ls '.$mibdir.' | grep -v traplist.txt',$output); |
||
| 209 | } |
||
| 210 | //$i=0;$listFiles='';while (isset($output[$i])) $listFiles.=$output[$i++]; |
||
| 211 | //$this->view->fileList=explode(' ',$listFiles); |
||
| 212 | $this->view->fileList=$output; |
||
| 213 | |||
| 214 | // Zend form |
||
| 215 | $this->view->form= new UploadForm(); |
||
| 216 | //$this->view->form= new Form('upload-form'); |
||
| 346 |