| 1 | /* |
|---|
| 2 | FILE |
|---|
| 3 | $Id$ |
|---|
| 4 | PURPOSE |
|---|
| 5 | Included in main.c. For command line interface and html main link. |
|---|
| 6 | |
|---|
| 7 | AUTHOR/LEGAL |
|---|
| 8 | (C) 2001-2011 Gary Wallis for Unixservice, LLC. |
|---|
| 9 | This software distributed under the GPLv2 license. |
|---|
| 10 | See LICENSE file included. |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #define macro_mySQLQueryErrorText mysql_query(&gMysql,gcQuery);\ |
|---|
| 15 | if(mysql_errno(&gMysql))\ |
|---|
| 16 | {\ |
|---|
| 17 | printf("%s\n",mysql_error(&gMysql));\ |
|---|
| 18 | printf("</td></tr>\n");\ |
|---|
| 19 | CloseFieldSet();\ |
|---|
| 20 | return;\ |
|---|
| 21 | }\ |
|---|
| 22 | mysqlRes=mysql_store_result(&gMysql); |
|---|
| 23 | |
|---|
| 24 | #define macro_mySQLQueryErrorText2 mysql_query(&gMysql,gcQuery);\ |
|---|
| 25 | if(mysql_errno(&gMysql))\ |
|---|
| 26 | {\ |
|---|
| 27 | printf("%s\n",mysql_error(&gMysql));\ |
|---|
| 28 | printf("</td></tr>\n");\ |
|---|
| 29 | CloseFieldSet();\ |
|---|
| 30 | return;\ |
|---|
| 31 | }\ |
|---|
| 32 | mysqlRes2=mysql_store_result(&gMysql); |
|---|
| 33 | |
|---|
| 34 | #include "local.h" |
|---|
| 35 | #include <dirent.h> |
|---|
| 36 | #include <openisp/ucidr.h> |
|---|
| 37 | void GetDatacenterProp(const unsigned uDatacenter,const char *cName,char *cValue);//tcontainerfunc.h |
|---|
| 38 | void SetContainerStatus(unsigned uContainer,unsigned uStatus); |
|---|
| 39 | void ChangeGroup(unsigned uContainer, unsigned uGroup); |
|---|
| 40 | void CreateDNSJob(unsigned uIPv4,unsigned uOwner,char const *cOptionalIPv4,char const *cHostname,unsigned uDatacenter,unsigned uCreatedBy); |
|---|
| 41 | void GetNodeProp(const unsigned uNode,const char *cName,char *cValue);//jobqueue.c |
|---|
| 42 | char *strptime(const char *s, const char *format, struct tm *tm); |
|---|
| 43 | |
|---|
| 44 | static char cTableList[64][32]={ "tAuthorize", "tClient", "tConfig", "tConfiguration", "tContainer", |
|---|
| 45 | "tDatacenter", "tGlossary", "tGroup", "tGroupGlue", "tGroupType", "tIP", "tJob", |
|---|
| 46 | "tJobStatus", "tLog", "tLogMonth", "tLogType", "tMonth", "tNameserver", "tNode", |
|---|
| 47 | "tOSTemplate", "tProperty", "tSearchdomain", "tStatus", "tTemplate", "tTemplateSet", |
|---|
| 48 | "tTemplateType", "tType", ""}; |
|---|
| 49 | |
|---|
| 50 | char cInitTableList[64][32]={ "tAuthorize", "tClient", "tConfig", "tGlossary", "tGroupType", |
|---|
| 51 | "tJobStatus", "tLogType", "tOSTemplate", "tStatus", "tTemplate", "tTemplateSet", |
|---|
| 52 | "tTemplateType", "tType", ""}; |
|---|
| 53 | |
|---|
| 54 | void ExtMainShell(int argc, char *argv[]); |
|---|
| 55 | void Initialize(char *cPasswd); |
|---|
| 56 | void Backup(char *cPasswd); |
|---|
| 57 | void Restore(char *cPasswd, char *cTableName); |
|---|
| 58 | void RestoreAll(char *cPasswd); |
|---|
| 59 | void mySQLRootConnect(char *cPasswd); |
|---|
| 60 | void ImportTemplateFile(char *cTemplate, char *cFile, char *cTemplateSet, char *cTemplateType); |
|---|
| 61 | void ImportOSTemplates(char *cPath,char *cOwner); |
|---|
| 62 | void MassCreateContainers(char *cConfigfileName); |
|---|
| 63 | void ImportRemoteDatacenter( |
|---|
| 64 | const char *cLocalDatacenter, |
|---|
| 65 | const char *cRemoteDatacenter, |
|---|
| 66 | const char *cLocalNode, |
|---|
| 67 | const char *cRemoteNode, |
|---|
| 68 | const char *cHost, |
|---|
| 69 | const char *cUser, |
|---|
| 70 | const char *cPasswd, |
|---|
| 71 | const char *cuOwner); |
|---|
| 72 | void ExtracttLog(char *cMonth, char *cYear, char *cPasswd, char *cTablePath); |
|---|
| 73 | time_t cDateToUnixTime(char *cDate); |
|---|
| 74 | void CreatetLogTable(char *cTableName); |
|---|
| 75 | void NextMonthYear(char *cMonth,char *cYear,char *cNextMonth,char *cNextYear); |
|---|
| 76 | |
|---|
| 77 | void CalledByAlias(int iArgc,char *cArgv[]); |
|---|
| 78 | unsigned TextConnectDb(void);//mysqlconnect.c |
|---|
| 79 | void DashBoard(const char *cOptionalMsg); |
|---|
| 80 | void CloneReport(const char *cOptionalMsg); |
|---|
| 81 | void ContainerReport(const char *cOptionalMsg); |
|---|
| 82 | void EncryptPasswdMD5(char *pw); |
|---|
| 83 | void GetConfiguration(const char *cName,char *cValue, |
|---|
| 84 | unsigned uDatacenter, |
|---|
| 85 | unsigned uNode, |
|---|
| 86 | unsigned uContainer, |
|---|
| 87 | unsigned uHtml); |
|---|
| 88 | void UpdateSchema(void); |
|---|
| 89 | void RecoverMode(void); |
|---|
| 90 | void ResetAllSyncPeriod(void); |
|---|
| 91 | void UpdateCloneHostnames(void); |
|---|
| 92 | |
|---|
| 93 | //jobqueue.c |
|---|
| 94 | void ProcessJobQueue(unsigned uDebug); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | int iExtMainCommands(pentry entries[], int x) |
|---|
| 98 | { |
|---|
| 99 | if(!strcmp(gcFunction,"MainTools")) |
|---|
| 100 | { |
|---|
| 101 | if(!strcmp(gcCommand,"CloneReport")) |
|---|
| 102 | { |
|---|
| 103 | unxsVZ("CloneReport"); |
|---|
| 104 | } |
|---|
| 105 | else if(!strcmp(gcCommand,"ContainerReport")) |
|---|
| 106 | { |
|---|
| 107 | unxsVZ("ContainerReport"); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | return(0); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | void CloneReport(const char *cOptionalMsg) |
|---|
| 115 | { |
|---|
| 116 | MYSQL_RES *mysqlRes; |
|---|
| 117 | MYSQL_ROW mysqlField; |
|---|
| 118 | MYSQL_RES *mysqlRes2; |
|---|
| 119 | MYSQL_ROW mysqlField2; |
|---|
| 120 | |
|---|
| 121 | char cuContainer[16]; |
|---|
| 122 | unsigned uContainer; |
|---|
| 123 | unsigned uCount=0; |
|---|
| 124 | char cuSyncPeriod[16]; |
|---|
| 125 | |
|---|
| 126 | //To handle error messages etc. |
|---|
| 127 | if(cOptionalMsg[0] && strcmp(cOptionalMsg,"CloneReport")) |
|---|
| 128 | { |
|---|
| 129 | printf("%s\n",cOptionalMsg); |
|---|
| 130 | return; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | OpenFieldSet("CloneReport",100); |
|---|
| 134 | |
|---|
| 135 | OpenRow("<u>Containers with no clone or with cuSyncPeriod=0</u>","black"); |
|---|
| 136 | sprintf(gcQuery,"SELECT tContainer.cLabel,tContainer.cHostname,tContainer.uContainer,tNode.cLabel,tDatacenter.cLabel" |
|---|
| 137 | " FROM tContainer,tNode,tDatacenter" |
|---|
| 138 | " WHERE tContainer.uNode=tNode.uNode AND tContainer.uDatacenter=tDatacenter.uDatacenter" |
|---|
| 139 | " AND tContainer.uSource=0 AND (tContainer.uStatus=1 OR tContainer.uStatus=31)" |
|---|
| 140 | " ORDER BY tContainer.cLabel,tContainer.uDatacenter,tContainer.uNode"); |
|---|
| 141 | macro_mySQLQueryErrorText |
|---|
| 142 | printf("</td></tr><tr><td></td><td><u>cLabel</u></td><td><u>cHostname</u></td>" |
|---|
| 143 | "<td><u>Node</td><td><u>Datacenter</u></td>\n"); |
|---|
| 144 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 145 | { |
|---|
| 146 | cuContainer[0]=0; |
|---|
| 147 | |
|---|
| 148 | sprintf(gcQuery,"SELECT uContainer FROM tContainer WHERE uSource=%s",mysqlField[2]); |
|---|
| 149 | macro_mySQLQueryErrorText2 |
|---|
| 150 | if((mysqlField2=mysql_fetch_row(mysqlRes2))) |
|---|
| 151 | sprintf(cuContainer,"%.15s",mysqlField2[0]); |
|---|
| 152 | mysql_free_result(mysqlRes2); |
|---|
| 153 | |
|---|
| 154 | if(!cuContainer[0]) |
|---|
| 155 | { |
|---|
| 156 | uCount++; |
|---|
| 157 | printf("<tr><td></td><td><a href=unxsVZ.cgi?gcFunction=tContainer&uContainer=%s>%s</a></td>" |
|---|
| 158 | "<td>%s</td><td>%s</td><td>%s</td>\n",mysqlField[2], |
|---|
| 159 | mysqlField[0],mysqlField[1],mysqlField[3],mysqlField[4]); |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | cuSyncPeriod[0]=0; |
|---|
| 164 | sscanf(mysqlField2[0],"%u",&uContainer); |
|---|
| 165 | GetContainerProp(uContainer,"cuSyncPeriod",cuSyncPeriod); |
|---|
| 166 | if(cuSyncPeriod[0] && cuSyncPeriod[0]=='0') |
|---|
| 167 | { |
|---|
| 168 | uCount++; |
|---|
| 169 | printf("<tr><td>Clone w/cuSyncPeriod=0</td><td>" |
|---|
| 170 | "<a href=unxsVZ.cgi?gcFunction=tContainer&uContainer=%u>%s<a></td>" |
|---|
| 171 | "<td>%s</td><td>%s</td><td>%s</td>\n",uContainer, |
|---|
| 172 | mysqlField[0],mysqlField[1],mysqlField[3],mysqlField[4]); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | mysql_free_result(mysqlRes); |
|---|
| 177 | //Lets add a count |
|---|
| 178 | printf("<tr><td>Total %u</td><td></td><td></td><td></td><td></td>\n",uCount); |
|---|
| 179 | |
|---|
| 180 | //1=Active 31=Stopped TODO |
|---|
| 181 | uCount=0; |
|---|
| 182 | OpenRow("<p>","black"); |
|---|
| 183 | OpenRow("<u>Containers with clones not updated in last hour</u>","black"); |
|---|
| 184 | sprintf(gcQuery,"SELECT tContainer.cLabel,tContainer.cHostname,tContainer.uContainer,tContainer.uNode," |
|---|
| 185 | "tContainer.uDatacenter,tDatacenter.cLabel FROM tContainer,tDatacenter WHERE" |
|---|
| 186 | " tContainer.uSource=0 AND (tContainer.uStatus=1 OR tContainer.uStatus=31) AND" |
|---|
| 187 | " tContainer.uDatacenter=tDatacenter.uDatacenter ORDER BY tContainer.cLabel"); |
|---|
| 188 | macro_mySQLQueryErrorText |
|---|
| 189 | printf("</td></tr><tr><td></td><td><u>Source cLabel</u></td>" |
|---|
| 190 | "<td><u>Clone cLabel</u><td><u>Source cHostname</u></td>" |
|---|
| 191 | "<td><u>Clone Status/cuSyncPeriod</u></td><td><u>Clone Node</td><td><u>Datacenter</u></td>\n"); |
|---|
| 192 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 193 | { |
|---|
| 194 | sprintf(gcQuery,"SELECT tContainer.uContainer,tStatus.cLabel,tContainer.cLabel,tContainer.cLabel," |
|---|
| 195 | "tNode.cLabel" |
|---|
| 196 | " FROM tContainer,tStatus,tNode WHERE" |
|---|
| 197 | " tContainer.uSource=%s AND tContainer.uBackupDate<(UNIX_TIMESTAMP(NOW())-3600)" |
|---|
| 198 | " AND tContainer.uStatus=tStatus.uStatus" |
|---|
| 199 | " AND tContainer.uNode=tNode.uNode", |
|---|
| 200 | mysqlField[2]); |
|---|
| 201 | macro_mySQLQueryErrorText2 |
|---|
| 202 | while((mysqlField2=mysql_fetch_row(mysqlRes2))) |
|---|
| 203 | { |
|---|
| 204 | uCount++; |
|---|
| 205 | sscanf(mysqlField2[0],"%u",&uContainer); |
|---|
| 206 | GetContainerProp(uContainer,"cuSyncPeriod",cuSyncPeriod); |
|---|
| 207 | printf("<tr><td></td><td><a href=unxsVZ.cgi?gcFunction=tContainer&uContainer=%s>%s</a></td>" |
|---|
| 208 | "<td><a href=unxsVZ.cgi?gcFunction=tContainer&uContainer=%s>%s</a></td>" |
|---|
| 209 | "<td>%s</td><td>%s/%s</a>" |
|---|
| 210 | "</td><td>%s</td><td>%s</td>\n",mysqlField[2],mysqlField[0], |
|---|
| 211 | mysqlField2[0],mysqlField2[2], |
|---|
| 212 | mysqlField[1],mysqlField2[1],cuSyncPeriod, |
|---|
| 213 | mysqlField2[4],mysqlField[5]); |
|---|
| 214 | } |
|---|
| 215 | mysql_free_result(mysqlRes2); |
|---|
| 216 | } |
|---|
| 217 | mysql_free_result(mysqlRes); |
|---|
| 218 | //Lets add a count |
|---|
| 219 | printf("<tr><td>Total %u</td><td></td><td></td><td></td><td></td>\n",uCount); |
|---|
| 220 | |
|---|
| 221 | uCount=0; |
|---|
| 222 | OpenRow("<p>","black"); |
|---|
| 223 | OpenRow("<u>Containers with remote clones</u>","black"); |
|---|
| 224 | sprintf(gcQuery,"SELECT tContainer.cLabel,tContainer.cHostname,tContainer.uContainer,tContainer.uNode," |
|---|
| 225 | "tContainer.uDatacenter,tDatacenter.cLabel FROM tContainer,tDatacenter WHERE" |
|---|
| 226 | " tContainer.uSource=0 AND (tContainer.uStatus=1 OR tContainer.uStatus=31) AND" |
|---|
| 227 | " tContainer.uDatacenter=tDatacenter.uDatacenter ORDER BY tContainer.cLabel"); |
|---|
| 228 | macro_mySQLQueryErrorText |
|---|
| 229 | printf("</td></tr><tr><td></td><td><u>Source cLabel</u></td>" |
|---|
| 230 | "<td><u>Clone cLabel</u><td><u>Source cHostname</u></td>" |
|---|
| 231 | "<td><u>Clone Status/cuSyncPeriod</u></td><td><u>Source Datacenter</td><td><u>Clone Datacenter</u></td>\n"); |
|---|
| 232 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 233 | { |
|---|
| 234 | sprintf(gcQuery,"SELECT tContainer.uContainer,tStatus.cLabel,tContainer.cLabel,tContainer.cLabel" |
|---|
| 235 | ",tDatacenter.cLabel" |
|---|
| 236 | " FROM tContainer,tStatus,tDatacenter WHERE" |
|---|
| 237 | " tContainer.uSource=%s AND tContainer.uDatacenter!=%s" |
|---|
| 238 | " AND tContainer.uStatus=tStatus.uStatus AND tContainer.uDatacenter=tDatacenter.uDatacenter", |
|---|
| 239 | mysqlField[2],mysqlField[4]); |
|---|
| 240 | macro_mySQLQueryErrorText2 |
|---|
| 241 | while((mysqlField2=mysql_fetch_row(mysqlRes2))) |
|---|
| 242 | { |
|---|
| 243 | uCount++; |
|---|
| 244 | sscanf(mysqlField2[0],"%u",&uContainer); |
|---|
| 245 | GetContainerProp(uContainer,"cuSyncPeriod",cuSyncPeriod); |
|---|
| 246 | printf("<tr><td></td><td><a href=unxsVZ.cgi?gcFunction=tContainer&uContainer=%s>%s</a></td>" |
|---|
| 247 | "<td><a href=unxsVZ.cgi?gcFunction=tContainer&uContainer=%s>%s</a></td>" |
|---|
| 248 | "<td>%s</td><td>%s/%s</a>" |
|---|
| 249 | "</td><td>%s</td><td>%s</td>\n",mysqlField[2],mysqlField[0], |
|---|
| 250 | mysqlField2[0],mysqlField2[2], |
|---|
| 251 | mysqlField[1],mysqlField2[1],cuSyncPeriod, |
|---|
| 252 | mysqlField[5],mysqlField2[4]); |
|---|
| 253 | } |
|---|
| 254 | mysql_free_result(mysqlRes2); |
|---|
| 255 | } |
|---|
| 256 | mysql_free_result(mysqlRes); |
|---|
| 257 | //Lets add a count |
|---|
| 258 | printf("<tr><td>Total %u</td><td></td><td></td><td></td><td></td>\n",uCount); |
|---|
| 259 | |
|---|
| 260 | CloseFieldSet(); |
|---|
| 261 | |
|---|
| 262 | }//void CloneReport(const char *cOptionalMsg) |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | void ContainerReport(const char *cOptionalMsg) |
|---|
| 266 | { |
|---|
| 267 | MYSQL_RES *mysqlRes; |
|---|
| 268 | MYSQL_ROW mysqlField; |
|---|
| 269 | |
|---|
| 270 | char cStatus[128]="Unknown"; |
|---|
| 271 | char cuProcesses[64]=""; |
|---|
| 272 | unsigned uContainer=0; |
|---|
| 273 | unsigned uProcesses=0; |
|---|
| 274 | unsigned uStatus=0; |
|---|
| 275 | |
|---|
| 276 | //To handle error messages etc. |
|---|
| 277 | if(cOptionalMsg[0] && strcmp(cOptionalMsg,"ContainerReport")) |
|---|
| 278 | { |
|---|
| 279 | printf("%s\n",cOptionalMsg); |
|---|
| 280 | return; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | OpenFieldSet("ContainerReport",100); |
|---|
| 284 | |
|---|
| 285 | OpenRow("<u>Container Comparison Report</u>","black"); |
|---|
| 286 | sprintf(gcQuery,"SELECT cLabel,cHostname,uContainer,uNode,uDatacenter,uStatus FROM tContainer " |
|---|
| 287 | " ORDER BY cLabel,uDatacenter,uNode"); |
|---|
| 288 | macro_mySQLQueryErrorText |
|---|
| 289 | printf("</td></tr><tr><td>(requires new veinfo.uProcesses)</td><td><u>uContainer</u></td><td><u>cLabel</u></td>" |
|---|
| 290 | "<td><u>cHostname</u>" |
|---|
| 291 | "</td><td><u>Status</u><td><u>Processes</u><td><u>uStatus</u></td></td><td><u>uNode</td>" |
|---|
| 292 | "<td><u>uDatacenter</u></td>\n"); |
|---|
| 293 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 294 | { |
|---|
| 295 | cuProcesses[0]=0; |
|---|
| 296 | |
|---|
| 297 | uContainer=0; |
|---|
| 298 | sscanf(mysqlField[2],"%u",&uContainer); |
|---|
| 299 | uStatus=0; |
|---|
| 300 | sscanf(mysqlField[5],"%u",&uStatus); |
|---|
| 301 | |
|---|
| 302 | if(!uContainer) continue; |
|---|
| 303 | |
|---|
| 304 | GetContainerProp(uContainer,"veinfo.uProcesses",cuProcesses); |
|---|
| 305 | if(cuProcesses[0]) |
|---|
| 306 | { |
|---|
| 307 | uProcesses=0; |
|---|
| 308 | sscanf(cuProcesses,"%u",&uProcesses); |
|---|
| 309 | |
|---|
| 310 | if(uProcesses && uStatus==1) |
|---|
| 311 | sprintf(cStatus,"Running"); |
|---|
| 312 | else if(uProcesses==0 && uStatus==31) |
|---|
| 313 | sprintf(cStatus,"Stopped"); |
|---|
| 314 | else if(uProcesses && uStatus==31) |
|---|
| 315 | sprintf(cStatus,"<font color=red>Inconsistency: Running but should not be!</font>"); |
|---|
| 316 | else if(uProcesses==0 && uStatus==1) |
|---|
| 317 | sprintf(cStatus,"<font color=red>Inconsistency: Stopped but should not be!</font>"); |
|---|
| 318 | else if(uProcesses && uStatus!=1) |
|---|
| 319 | sprintf(cStatus,"Transitional state"); |
|---|
| 320 | else if(1) |
|---|
| 321 | sprintf(cStatus,"Unexpected status"); |
|---|
| 322 | |
|---|
| 323 | printf("<tr><td></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%u</td><td>%u</td><td>%s</td>" |
|---|
| 324 | "<td>%s</td>\n", |
|---|
| 325 | mysqlField[2],mysqlField[0],mysqlField[1], |
|---|
| 326 | cStatus, |
|---|
| 327 | uProcesses, |
|---|
| 328 | uStatus, |
|---|
| 329 | mysqlField[3],mysqlField[4]); |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | mysql_free_result(mysqlRes); |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | CloseFieldSet(); |
|---|
| 336 | |
|---|
| 337 | }//void ContainerReport(const char *cOptionalMsg) |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | void DashBoard(const char *cOptionalMsg) |
|---|
| 341 | { |
|---|
| 342 | |
|---|
| 343 | MYSQL_RES *mysqlRes; |
|---|
| 344 | MYSQL_ROW mysqlField; |
|---|
| 345 | time_t luClock; |
|---|
| 346 | char cConfigBuffer[256]={""}; |
|---|
| 347 | |
|---|
| 348 | long unsigned luTotalFailcnt=1; |
|---|
| 349 | long unsigned luFailcnt=0; |
|---|
| 350 | long unsigned luTotalUsage=1,luTotalSoftLimit=1; |
|---|
| 351 | double fRatio; |
|---|
| 352 | char *cColor={""}; |
|---|
| 353 | |
|---|
| 354 | //To handle error messages etc. |
|---|
| 355 | if(cOptionalMsg[0] && strcmp(cOptionalMsg,"DashBoard")) |
|---|
| 356 | { |
|---|
| 357 | printf("%s\n",cOptionalMsg); |
|---|
| 358 | return; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | OpenFieldSet("Dashboard",100); |
|---|
| 362 | |
|---|
| 363 | GetConfiguration("DashGraph0",cConfigBuffer,0,0,0,0);//any server, txt error msg |
|---|
| 364 | if(cConfigBuffer[0]) |
|---|
| 365 | { |
|---|
| 366 | char cURL[256]={""}; |
|---|
| 367 | |
|---|
| 368 | OpenRow("DashGraphs","black"); |
|---|
| 369 | printf("</td></tr>\n"); |
|---|
| 370 | |
|---|
| 371 | GetConfiguration("DashGraph0URL",cURL,0,0,0,0); |
|---|
| 372 | if(cConfigBuffer[0] && cURL[0]) |
|---|
| 373 | printf("<tr><td></td><td colspan=3><a href=%s><img src=%s border=0></a>\n", |
|---|
| 374 | cURL,cConfigBuffer); |
|---|
| 375 | else if(cConfigBuffer[0]) |
|---|
| 376 | printf("<tr><td></td><td colspan=3><img src=%s>\n",cConfigBuffer); |
|---|
| 377 | |
|---|
| 378 | cURL[0]=0; |
|---|
| 379 | cConfigBuffer[0]=0; |
|---|
| 380 | GetConfiguration("DashGraph1",cConfigBuffer,0,0,0,0); |
|---|
| 381 | GetConfiguration("DashGraph1URL",cURL,0,0,0,0); |
|---|
| 382 | if(cConfigBuffer[0] && cURL[0]) |
|---|
| 383 | printf("<a href=%s><img src=%s border=0></a>\n",cURL,cConfigBuffer); |
|---|
| 384 | else if(cConfigBuffer[0]) |
|---|
| 385 | printf("<img src=%s>\n",cConfigBuffer); |
|---|
| 386 | |
|---|
| 387 | cURL[0]=0; |
|---|
| 388 | cConfigBuffer[0]=0; |
|---|
| 389 | GetConfiguration("DashGraph2",cConfigBuffer,0,0,0,0); |
|---|
| 390 | GetConfiguration("DashGraph2URL",cURL,0,0,0,0); |
|---|
| 391 | if(cConfigBuffer[0] && cURL[0]) |
|---|
| 392 | printf("<a href=%s><img src=%s border=0></a>\n",cURL,cConfigBuffer); |
|---|
| 393 | else if(cConfigBuffer[0]) |
|---|
| 394 | printf("<img src=%s>\n",cConfigBuffer); |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | printf("</td>"); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | // |
|---|
| 403 | OpenRow("Global Cluster Health","black"); |
|---|
| 404 | sprintf(gcQuery,"SELECT COUNT(uProperty) FROM tProperty WHERE cName LIKE '%%.luFailDelta'"); |
|---|
| 405 | macro_mySQLQueryErrorText |
|---|
| 406 | printf("</td></tr>\n"); |
|---|
| 407 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 408 | { |
|---|
| 409 | sscanf(mysqlField[0],"%lu",&luTotalFailcnt); |
|---|
| 410 | } |
|---|
| 411 | mysql_free_result(mysqlRes); |
|---|
| 412 | |
|---|
| 413 | sprintf(gcQuery,"SELECT COUNT(uProperty) FROM tProperty WHERE cName LIKE '%%.luFailDelta' AND cValue!='0'"); |
|---|
| 414 | macro_mySQLQueryErrorText |
|---|
| 415 | printf("</td></tr>\n"); |
|---|
| 416 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 417 | { |
|---|
| 418 | sscanf(mysqlField[0],"%lu",&luFailcnt); |
|---|
| 419 | } |
|---|
| 420 | mysql_free_result(mysqlRes); |
|---|
| 421 | if(luTotalFailcnt==0) luTotalFailcnt=1; |
|---|
| 422 | fRatio= ((float) luFailcnt / (float) luTotalFailcnt) * 100.00; |
|---|
| 423 | if(fRatio==0.0) |
|---|
| 424 | cColor="green"; |
|---|
| 425 | else if(fRatio<1.5) |
|---|
| 426 | cColor="teal"; |
|---|
| 427 | else if(fRatio<2.0) |
|---|
| 428 | cColor="yellow"; |
|---|
| 429 | else if(fRatio<5.0) |
|---|
| 430 | cColor="fuchsia"; |
|---|
| 431 | else if(fRatio>=5.0) |
|---|
| 432 | cColor="red"; |
|---|
| 433 | printf("<tr><td></td><td>Current luFailDelta Ratio %2.2f%%</td><td>%lu/%lu</td><td bgcolor=%s colspan=2></td></tr>\n", |
|---|
| 434 | fRatio,luFailcnt,luTotalFailcnt,cColor); |
|---|
| 435 | // |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | // |
|---|
| 439 | luTotalFailcnt=1; |
|---|
| 440 | sprintf(gcQuery,"SELECT COUNT(uProperty) FROM tProperty WHERE cName LIKE '%%.luFailcnt'"); |
|---|
| 441 | macro_mySQLQueryErrorText |
|---|
| 442 | printf("</td></tr>\n"); |
|---|
| 443 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 444 | { |
|---|
| 445 | sscanf(mysqlField[0],"%lu",&luTotalFailcnt); |
|---|
| 446 | } |
|---|
| 447 | mysql_free_result(mysqlRes); |
|---|
| 448 | |
|---|
| 449 | //sprintf(gcQuery,"SELECT COUNT(uProperty) FROM tProperty WHERE cName LIKE '%%.luFailcnt' AND cValue!='0'"); |
|---|
| 450 | sprintf(gcQuery,"select count(uLog) from tLog where cMessage like '%%>%% %%:%%'" |
|---|
| 451 | " and uCreatedDate>(unix_timestamp(now())-(86400*7))"); |
|---|
| 452 | macro_mySQLQueryErrorText |
|---|
| 453 | printf("</td></tr>\n"); |
|---|
| 454 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 455 | { |
|---|
| 456 | sscanf(mysqlField[0],"%lu",&luFailcnt); |
|---|
| 457 | } |
|---|
| 458 | if(luTotalFailcnt==0) luTotalFailcnt=1; |
|---|
| 459 | mysql_free_result(mysqlRes); |
|---|
| 460 | if(luTotalFailcnt==0) luTotalFailcnt=1; |
|---|
| 461 | fRatio= ((float) luFailcnt / (float) luTotalFailcnt) * 100.00; |
|---|
| 462 | if(fRatio<1.0) |
|---|
| 463 | cColor="green"; |
|---|
| 464 | else if(fRatio<2.0) |
|---|
| 465 | cColor="teal"; |
|---|
| 466 | else if(fRatio<5.0) |
|---|
| 467 | cColor="yellow"; |
|---|
| 468 | else if(fRatio<10.0) |
|---|
| 469 | cColor="fuchsia"; |
|---|
| 470 | else if(fRatio>=10.0) |
|---|
| 471 | cColor="red"; |
|---|
| 472 | printf("<tr><td></td><td>Last 7 Day <i>failcnt</i> Ratio %2.2f%%</td><td>%lu/%lu</td><td bgcolor=%s colspan=2></td></tr>\n", |
|---|
| 473 | fRatio,luFailcnt,luTotalFailcnt,cColor); |
|---|
| 474 | // |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | sprintf(gcQuery,"SELECT SUM(CONVERT(cValue,UNSIGNED)) FROM tProperty WHERE cName='1k-blocks.luUsage'"); |
|---|
| 478 | macro_mySQLQueryErrorText |
|---|
| 479 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 480 | { |
|---|
| 481 | if(mysqlField[0]!=NULL) |
|---|
| 482 | sscanf(mysqlField[0],"%lu",&luTotalUsage); |
|---|
| 483 | } |
|---|
| 484 | mysql_free_result(mysqlRes); |
|---|
| 485 | |
|---|
| 486 | sprintf(gcQuery,"SELECT SUM(CONVERT(cValue,UNSIGNED)) FROM tProperty WHERE cName='1k-blocks.luSoftlimit'"); |
|---|
| 487 | macro_mySQLQueryErrorText |
|---|
| 488 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 489 | { |
|---|
| 490 | if(mysqlField[0]!=NULL) |
|---|
| 491 | sscanf(mysqlField[0],"%lu",&luTotalSoftLimit); |
|---|
| 492 | } |
|---|
| 493 | mysql_free_result(mysqlRes); |
|---|
| 494 | if(luTotalSoftLimit==0) luTotalSoftLimit=1; |
|---|
| 495 | fRatio= ((float) luTotalUsage/ (float) luTotalSoftLimit) * 100.00 ; |
|---|
| 496 | if(fRatio<20.00) |
|---|
| 497 | cColor="green"; |
|---|
| 498 | else if(fRatio<30.00) |
|---|
| 499 | cColor="teal"; |
|---|
| 500 | else if(fRatio<40.00) |
|---|
| 501 | cColor="yellow"; |
|---|
| 502 | else if(fRatio<50.00) |
|---|
| 503 | cColor="fuchsia"; |
|---|
| 504 | else if(fRatio>=50.00) |
|---|
| 505 | cColor="red"; |
|---|
| 506 | |
|---|
| 507 | if(luTotalSoftLimit==1) |
|---|
| 508 | printf("<tr><td></td><td>No Container Usage Ratio</td>" |
|---|
| 509 | "<td>%luG/%luG</td><td bgcolor=green colspan=2></td></tr>\n", |
|---|
| 510 | luTotalUsage/1048576,luTotalSoftLimit/1048576); |
|---|
| 511 | else |
|---|
| 512 | printf("<tr><td></td><td>All Container Usage Ratio %2.2f%%</td>" |
|---|
| 513 | "<td>%luG/%luG</td><td bgcolor=%s colspan=2></td></tr>\n", |
|---|
| 514 | fRatio,luTotalUsage/1048576,luTotalSoftLimit/1048576,cColor); |
|---|
| 515 | // |
|---|
| 516 | // |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | if(guPermLevel>11 && guLoginClient==1) |
|---|
| 520 | { |
|---|
| 521 | OpenRow("System Messages (Last 10)","black"); |
|---|
| 522 | sprintf(gcQuery,"SELECT cMessage,GREATEST(uCreatedDate,uModDate),cServer FROM tLog" |
|---|
| 523 | " WHERE uLogType=4 ORDER BY GREATEST(uCreatedDate,uModDate) DESC LIMIT 10"); |
|---|
| 524 | macro_mySQLQueryErrorText |
|---|
| 525 | printf("</td></tr>\n"); |
|---|
| 526 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 527 | { |
|---|
| 528 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 529 | printf("<tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>\n", |
|---|
| 530 | ctime(&luClock),mysqlField[0],mysqlField[2]); |
|---|
| 531 | } |
|---|
| 532 | mysql_free_result(mysqlRes); |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | OpenRow("tLog (Last 10)","black"); |
|---|
| 536 | sprintf(gcQuery,"SELECT tLog.cLabel,GREATEST(tLog.uCreatedDate,tLog.uModDate),tLog.cLogin," |
|---|
| 537 | "tLog.cTableName,tLog.cHost,tLogType.cLabel FROM tLog,tLogType WHERE " |
|---|
| 538 | "tLog.uLogType=tLogType.uLogType AND tLog.uLogType!=4 AND tLog.uLogType!=6 " |
|---|
| 539 | "ORDER BY GREATEST(tLog.uCreatedDate,tLog.uModDate) DESC LIMIT 10"); |
|---|
| 540 | macro_mySQLQueryErrorText |
|---|
| 541 | printf("</td></tr>\n"); |
|---|
| 542 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 543 | { |
|---|
| 544 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 545 | printf("<tr><td></td><td>%s</td><td>%s %s</td><td>%s %s</td><td>%s</td></tr>\n", |
|---|
| 546 | ctime(&luClock),mysqlField[0],mysqlField[3],mysqlField[5],mysqlField[2],mysqlField[4]); |
|---|
| 547 | } |
|---|
| 548 | mysql_free_result(mysqlRes); |
|---|
| 549 | |
|---|
| 550 | OpenRow("Login Activity (Last 10)","black"); |
|---|
| 551 | sprintf(gcQuery,"SELECT cLabel,GREATEST(uCreatedDate,uModDate),cServer,cHost FROM" |
|---|
| 552 | " tLog WHERE uLogType=6 ORDER BY GREATEST(uCreatedDate,uModDate)" |
|---|
| 553 | " DESC LIMIT 10"); |
|---|
| 554 | macro_mySQLQueryErrorText |
|---|
| 555 | printf("</td></tr>\n"); |
|---|
| 556 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 557 | { |
|---|
| 558 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 559 | printf("<td></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", |
|---|
| 560 | ctime(&luClock),mysqlField[0],mysqlField[2],mysqlField[3]); |
|---|
| 561 | } |
|---|
| 562 | mysql_free_result(mysqlRes); |
|---|
| 563 | } |
|---|
| 564 | else |
|---|
| 565 | { |
|---|
| 566 | OpenRow("System Messages (Last 10)","black"); |
|---|
| 567 | sprintf(gcQuery,"SELECT cMessage,GREATEST(uCreatedDate,uModDate),cServer FROM tLog" |
|---|
| 568 | " WHERE uLogType=4 AND uOwner=%u ORDER BY GREATEST(uCreatedDate,uModDate)" |
|---|
| 569 | " DESC LIMIT 10",guCompany); |
|---|
| 570 | macro_mySQLQueryErrorText |
|---|
| 571 | printf("</td></tr>\n"); |
|---|
| 572 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 573 | { |
|---|
| 574 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 575 | printf("<tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>\n", |
|---|
| 576 | ctime(&luClock),mysqlField[0],mysqlField[2]); |
|---|
| 577 | } |
|---|
| 578 | mysql_free_result(mysqlRes); |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | OpenRow("tLog (Last 10)","black"); |
|---|
| 582 | sprintf(gcQuery,"SELECT tLog.cLabel,GREATEST(tLog.uCreatedDate,tLog.uModDate),tLog.cLogin," |
|---|
| 583 | "tLog.cTableName,tLog.cHost,tLogType.cLabel FROM tLog,tLogType WHERE " |
|---|
| 584 | "tLog.uLogType=tLogType.uLogType AND tLog.uLogType!=4 AND tLog.uLogType!=6 AND" |
|---|
| 585 | " tLog.uOwner=%u ORDER BY GREATEST(tLog.uCreatedDate,tLog.uModDate) DESC LIMIT 10", |
|---|
| 586 | guCompany); |
|---|
| 587 | macro_mySQLQueryErrorText |
|---|
| 588 | printf("</td></tr>\n"); |
|---|
| 589 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 590 | { |
|---|
| 591 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 592 | printf("<tr><td></td><td>%s</td><td>%s %s</td><td>%s %s</td><td>%s</td></tr>\n", |
|---|
| 593 | ctime(&luClock),mysqlField[0],mysqlField[3],mysqlField[5],mysqlField[2],mysqlField[4]); |
|---|
| 594 | } |
|---|
| 595 | mysql_free_result(mysqlRes); |
|---|
| 596 | |
|---|
| 597 | OpenRow("Login Activity (Last 10)","black"); |
|---|
| 598 | sprintf(gcQuery,"SELECT cLabel,GREATEST(uCreatedDate,uModDate),cServer,cHost FROM" |
|---|
| 599 | " tLog WHERE uLogType=6 AND uOwner=%u ORDER BY GREATEST(uCreatedDate,uModDate)" |
|---|
| 600 | " DESC LIMIT 10",guCompany); |
|---|
| 601 | macro_mySQLQueryErrorText |
|---|
| 602 | printf("</td></tr>\n"); |
|---|
| 603 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 604 | { |
|---|
| 605 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 606 | printf("<td></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", |
|---|
| 607 | ctime(&luClock),mysqlField[0],mysqlField[2],mysqlField[3]); |
|---|
| 608 | } |
|---|
| 609 | mysql_free_result(mysqlRes); |
|---|
| 610 | }//end of if(guPermLevel>11 ...) |
|---|
| 611 | |
|---|
| 612 | OpenRow("Pending or Stuck Jobs (Last 10)","black"); |
|---|
| 613 | if(guPermLevel>11 && guLoginClient==1) |
|---|
| 614 | sprintf(gcQuery,"SELECT tJob.cLabel,GREATEST(tJob.uCreatedDate,tJob.uModDate),tNode.cLabel,tJobStatus.cLabel" |
|---|
| 615 | " FROM tJob,tJobStatus,tNode WHERE tJob.uNode=tNode.uNode AND" |
|---|
| 616 | " tJob.uJobStatus=tJobStatus.uJobStatus AND tJob.uJobStatus!=3" |
|---|
| 617 | " AND tJob.uJobStatus!=7 AND tJob.uJobStatus!=6" |
|---|
| 618 | " ORDER BY GREATEST(tJob.uCreatedDate,tJob.uModDate) DESC LIMIT 10"); |
|---|
| 619 | else |
|---|
| 620 | sprintf(gcQuery,"SELECT tJob.cLabel,GREATEST(tJob.uCreatedDate,tJob.uModDate),tNode.cLabel,tJobStatus.cLabel" |
|---|
| 621 | " FROM tJob,tJobStatus,tNode WHERE tJob.uNode=tNode.uNode AND" |
|---|
| 622 | " tJob.uJobStatus=tJobStatus.uJobStatus AND tJob.uJobStatus!=3" |
|---|
| 623 | " AND tJob.uJobStatus!=7 AND tJob.uJobStatus!=6 AND tJob.uOwner=%u" |
|---|
| 624 | " ORDER BY GREATEST(tJob.uCreatedDate,tJob.uModDate) DESC LIMIT 10",guCompany); |
|---|
| 625 | macro_mySQLQueryErrorText |
|---|
| 626 | printf("</td></tr>\n"); |
|---|
| 627 | while((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 628 | { |
|---|
| 629 | sscanf(mysqlField[1],"%lu",&luClock); |
|---|
| 630 | printf("<tr><td></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", |
|---|
| 631 | ctime(&luClock),mysqlField[0],mysqlField[2],mysqlField[3]); |
|---|
| 632 | } |
|---|
| 633 | mysql_free_result(mysqlRes); |
|---|
| 634 | |
|---|
| 635 | CloseFieldSet(); |
|---|
| 636 | |
|---|
| 637 | }//void DashBoard(const char *cOptionalMsg) |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | void ExtMainContent(void) |
|---|
| 641 | { |
|---|
| 642 | printf("<p><input type=hidden name=gcFunction value=MainTools>"); |
|---|
| 643 | |
|---|
| 644 | OpenFieldSet("System Information",100); |
|---|
| 645 | |
|---|
| 646 | OpenRow("Hostname","black"); |
|---|
| 647 | printf("<td>%s</td></tr>\n",gcHostname); |
|---|
| 648 | |
|---|
| 649 | OpenRow("Build Information","black"); |
|---|
| 650 | printf("<td>%s</td></tr>\n",gcBuildInfo); |
|---|
| 651 | |
|---|
| 652 | OpenRow("RAD Status","black"); |
|---|
| 653 | printf("<td>%s %s</td></tr>\n",gcRADStatus,REV); |
|---|
| 654 | |
|---|
| 655 | OpenRow("Application Summary","black"); |
|---|
| 656 | printf("<td>Manages OpenVZ containers across datacenters and hardware nodes. Flexible and" |
|---|
| 657 | " extensible configuration management. Cloning, migration, duplication and backup" |
|---|
| 658 | " operations supported, across both nodes and datacenters.</td></tr>\n"); |
|---|
| 659 | if(guPermLevel>9) |
|---|
| 660 | { |
|---|
| 661 | register unsigned int i; |
|---|
| 662 | OpenRow("Table List","black"); |
|---|
| 663 | printf("<td>\n"); |
|---|
| 664 | for(i=0;cTableList[i][0];i++) |
|---|
| 665 | printf("<a href=unxsVZ.cgi?gcFunction=%.32s>%.32s</a><br>\n", |
|---|
| 666 | cTableList[i],cTableList[i]); |
|---|
| 667 | printf("</td></tr>\n"); |
|---|
| 668 | OpenRow("Admin Functions","black"); |
|---|
| 669 | printf("<td><input type=hidden name=gcFunction value=MainTools>\n"); |
|---|
| 670 | printf(" <input title='Find containers not being cloned' class=largeButton type=submit name=gcCommand" |
|---|
| 671 | " value=CloneReport > \n"); |
|---|
| 672 | printf(" <input title='Find inconsistencies between actual node vz containers and unxsVZ status'" |
|---|
| 673 | " class=largeButton type=submit name=gcCommand value=ContainerReport ></td></tr>\n"); |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | CloseFieldSet(); |
|---|
| 677 | |
|---|
| 678 | }//void ExtMainContent(void) |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | //If called from command line |
|---|
| 682 | void ExtMainShell(int argc, char *argv[]) |
|---|
| 683 | { |
|---|
| 684 | if(getuid()) |
|---|
| 685 | { |
|---|
| 686 | printf("Only root can run this command!\n"); |
|---|
| 687 | exit(0); |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | if(argc==2 && !strcmp(argv[1],"ProcessJobQueue")) |
|---|
| 691 | ProcessJobQueue(0); |
|---|
| 692 | if(argc==2 && !strcmp(argv[1],"ProcessJobQueueDebug")) |
|---|
| 693 | ProcessJobQueue(1); |
|---|
| 694 | else if(argc==2 && !strcmp(argv[1],"UpdateSchema")) |
|---|
| 695 | UpdateSchema(); |
|---|
| 696 | else if(argc==2 && !strcmp(argv[1],"UpdateCloneHostnames")) |
|---|
| 697 | UpdateCloneHostnames(); |
|---|
| 698 | else if(argc==2 && !strcmp(argv[1],"RecoverMode")) |
|---|
| 699 | RecoverMode(); |
|---|
| 700 | else if(argc==2 && !strcmp(argv[1],"ResetAllSyncPeriod")) |
|---|
| 701 | ResetAllSyncPeriod(); |
|---|
| 702 | else if(argc==6 && !strcmp(argv[1],"ImportTemplateFile")) |
|---|
| 703 | ImportTemplateFile(argv[2],argv[3],argv[4],argv[5]); |
|---|
| 704 | else if(argc==3 && !strcmp(argv[1],"Initialize")) |
|---|
| 705 | Initialize(argv[2]); |
|---|
| 706 | else if(argc==3 && !strcmp(argv[1],"Backup")) |
|---|
| 707 | Backup(argv[2]); |
|---|
| 708 | else if(argc==4 && !strcmp(argv[1],"Restore")) |
|---|
| 709 | Restore(argv[2],argv[3]); |
|---|
| 710 | else if(argc==3 && !strcmp(argv[1],"RestoreAll")) |
|---|
| 711 | RestoreAll(argv[2]); |
|---|
| 712 | else if(argc==6 && !strcmp(argv[1],"ExtracttLog")) |
|---|
| 713 | ExtracttLog(argv[2],argv[3],argv[4],argv[5]); |
|---|
| 714 | else if(argc==10 && !strcmp(argv[1],"ImportRemoteDatacenter")) |
|---|
| 715 | ImportRemoteDatacenter(argv[2],argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9]); |
|---|
| 716 | else if(argc==4 && !strcmp(argv[1],"ImportOSTemplates")) |
|---|
| 717 | ImportOSTemplates(argv[2],argv[3]); |
|---|
| 718 | else if(argc==3 && !strcmp(argv[1],"MassCreateContainers")) |
|---|
| 719 | MassCreateContainers(argv[2]); |
|---|
| 720 | else |
|---|
| 721 | { |
|---|
| 722 | printf("\n%s %s Menu\n\nDatabase Ops:\n",argv[0],RELEASE); |
|---|
| 723 | printf("\tInitialize|Backup|RestoreAll <mysql root passwd>\n"); |
|---|
| 724 | printf("\tRestore <mysql root passwd> <Restore table name>\n"); |
|---|
| 725 | printf("\nCrontab Ops:\n"); |
|---|
| 726 | printf("\tProcessJobQueue\n"); |
|---|
| 727 | printf("\tProcessJobQueueDebug\n"); |
|---|
| 728 | //printf("\tProcessExtJobQueue <cServer>\n"); |
|---|
| 729 | printf("\nSpecial Admin Ops:\n"); |
|---|
| 730 | printf("\tUpdateSchema\n"); |
|---|
| 731 | printf("\tUpdateCloneHostnames\n"); |
|---|
| 732 | printf("\tRecoverMode\n"); |
|---|
| 733 | printf("\tResetAllSyncPeriod\n"); |
|---|
| 734 | printf("\tImportTemplateFile <tTemplate.cLabel> <filespec> <tTemplateSet.cLabel> <tTemplateType.cLabel>\n"); |
|---|
| 735 | printf("\tExtracttLog <Mon> <Year> <mysql root passwd> <path to mysql table>\n"); |
|---|
| 736 | printf("\tImportRemoteDatacenter <local datacenter> <remote datacenter> <local node> <remote node>\n" |
|---|
| 737 | "\t\t<host> <user> <passwd> <local uOwner>\n"); |
|---|
| 738 | printf("\tImportOSTemplates <path to templates e.g. /vz/template/cache/> <tClient.cLabel owner string>\n"); |
|---|
| 739 | printf("\tMassCreateContainers <configuration file>\n"); |
|---|
| 740 | printf("\n"); |
|---|
| 741 | } |
|---|
| 742 | mysql_close(&gMysql); |
|---|
| 743 | exit(0); |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | }//void ExtMainShell(int argc, char *argv[]) |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | //ProjectFunctionStubs() |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | void RestoreAll(char *cPasswd) |
|---|
| 753 | { |
|---|
| 754 | char cISMROOT[256]={""}; |
|---|
| 755 | register int i; |
|---|
| 756 | |
|---|
| 757 | if(getenv("ISMROOT")!=NULL) |
|---|
| 758 | { |
|---|
| 759 | strncpy(cISMROOT,getenv("ISMROOT"),255); |
|---|
| 760 | gcHost[255]=0; |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | if(!cISMROOT[0]) |
|---|
| 764 | { |
|---|
| 765 | printf("You must set ISMROOT env var first. Ex. export ISMROOT=/home/joe\n"); |
|---|
| 766 | exit(1); |
|---|
| 767 | } |
|---|
| 768 | |
|---|
| 769 | printf("Restoring unxsVZ data from .txt file in %s/unxsVZ/data...\n\n",cISMROOT); |
|---|
| 770 | |
|---|
| 771 | //connect as root to master db |
|---|
| 772 | mySQLRootConnect(cPasswd); |
|---|
| 773 | |
|---|
| 774 | sprintf(gcQuery,"USE %s",DBNAME); |
|---|
| 775 | mysql_query(&gMysql,gcQuery); |
|---|
| 776 | if(mysql_errno(&gMysql)) |
|---|
| 777 | { |
|---|
| 778 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 779 | exit(1); |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | for(i=0;cTableList[i][0];i++) |
|---|
| 783 | { |
|---|
| 784 | sprintf(gcQuery,"LOAD DATA LOCAL INFILE '%s/unxsVZ/data/%s.txt' REPLACE INTO TABLE %s", |
|---|
| 785 | cISMROOT,cTableList[i],cTableList[i]); |
|---|
| 786 | mysql_query(&gMysql,gcQuery); |
|---|
| 787 | if(mysql_errno(&gMysql)) |
|---|
| 788 | { |
|---|
| 789 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 790 | exit(1); |
|---|
| 791 | } |
|---|
| 792 | printf("%s\n",cTableList[i]); |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | printf("\nDone\n"); |
|---|
| 796 | |
|---|
| 797 | }//void RestoreAll(char *cPasswd) |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | void Restore(char *cPasswd, char *cTableName) |
|---|
| 801 | { |
|---|
| 802 | char cISMROOT[256]={""}; |
|---|
| 803 | |
|---|
| 804 | if(getenv("ISMROOT")!=NULL) |
|---|
| 805 | { |
|---|
| 806 | strncpy(cISMROOT,getenv("ISMROOT"),255); |
|---|
| 807 | gcHost[255]=0; |
|---|
| 808 | } |
|---|
| 809 | |
|---|
| 810 | if(!cISMROOT[0]) |
|---|
| 811 | { |
|---|
| 812 | printf("You must set ISMROOT env var first. Ex. export ISMROOT=/home/joe\n"); |
|---|
| 813 | exit(1); |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | printf("Restoring unxsVZ data from .txt file in %s/unxsVZ/data...\n\n",cISMROOT); |
|---|
| 817 | |
|---|
| 818 | //connect as root to master db |
|---|
| 819 | mySQLRootConnect(cPasswd); |
|---|
| 820 | |
|---|
| 821 | sprintf(gcQuery,"USE %s",DBNAME); |
|---|
| 822 | mysql_query(&gMysql,gcQuery); |
|---|
| 823 | if(mysql_errno(&gMysql)) |
|---|
| 824 | { |
|---|
| 825 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 826 | exit(1); |
|---|
| 827 | } |
|---|
| 828 | |
|---|
| 829 | sprintf(gcQuery,"LOAD DATA LOCAL INFILE '%s/unxsVZ/data/%s.txt' REPLACE INTO TABLE %s", |
|---|
| 830 | cISMROOT,cTableName,cTableName); |
|---|
| 831 | mysql_query(&gMysql,gcQuery); |
|---|
| 832 | if(mysql_errno(&gMysql)) |
|---|
| 833 | { |
|---|
| 834 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 835 | exit(1); |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | printf("%s\n\nDone\n",cTableName); |
|---|
| 839 | |
|---|
| 840 | }//void Restore(char *cPasswd, char *cTableName) |
|---|
| 841 | |
|---|
| 842 | |
|---|
| 843 | void Backup(char *cPasswd) |
|---|
| 844 | { |
|---|
| 845 | register int i; |
|---|
| 846 | char cISMROOT[256]={""}; |
|---|
| 847 | |
|---|
| 848 | if(getenv("ISMROOT")!=NULL) |
|---|
| 849 | { |
|---|
| 850 | strncpy(cISMROOT,getenv("ISMROOT"),255); |
|---|
| 851 | gcHost[255]=0; |
|---|
| 852 | } |
|---|
| 853 | |
|---|
| 854 | if(!cISMROOT[0]) |
|---|
| 855 | { |
|---|
| 856 | printf("You must set ISMROOT env var first. Ex. export ISMROOT=/home/joe\n"); |
|---|
| 857 | exit(1); |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | printf("Backing up unxsVZ data to .txt files in %s/unxsVZ/data...\n\n",cISMROOT); |
|---|
| 861 | |
|---|
| 862 | //connect as root to master db |
|---|
| 863 | mySQLRootConnect(cPasswd); |
|---|
| 864 | |
|---|
| 865 | sprintf(gcQuery,"USE %s",DBNAME); |
|---|
| 866 | mysql_query(&gMysql,gcQuery); |
|---|
| 867 | if(mysql_errno(&gMysql)) |
|---|
| 868 | { |
|---|
| 869 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 870 | exit(1); |
|---|
| 871 | } |
|---|
| 872 | |
|---|
| 873 | for(i=0;cTableList[i][0];i++) |
|---|
| 874 | { |
|---|
| 875 | char cFileName[300]; |
|---|
| 876 | |
|---|
| 877 | sprintf(cFileName,"%s/unxsVZ/data/%s.txt" |
|---|
| 878 | ,cISMROOT,cTableList[i]); |
|---|
| 879 | unlink(cFileName); |
|---|
| 880 | |
|---|
| 881 | sprintf(gcQuery,"SELECT * INTO OUTFILE '%s' FROM %s", |
|---|
| 882 | cFileName,cTableList[i]); |
|---|
| 883 | mysql_query(&gMysql,gcQuery); |
|---|
| 884 | if(mysql_errno(&gMysql)) |
|---|
| 885 | { |
|---|
| 886 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 887 | exit(1); |
|---|
| 888 | } |
|---|
| 889 | printf("%s\n",cTableList[i]); |
|---|
| 890 | } |
|---|
| 891 | |
|---|
| 892 | |
|---|
| 893 | printf("\nDone.\n"); |
|---|
| 894 | |
|---|
| 895 | }//void Backup(char *cPasswd) |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | void Initialize(char *cPasswd) |
|---|
| 899 | { |
|---|
| 900 | char cISMROOT[256]={""}; |
|---|
| 901 | register int i; |
|---|
| 902 | |
|---|
| 903 | if(getenv("ISMROOT")!=NULL) |
|---|
| 904 | { |
|---|
| 905 | strncpy(cISMROOT,getenv("ISMROOT"),255); |
|---|
| 906 | gcHost[255]=0; |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | if(!cISMROOT[0]) |
|---|
| 910 | { |
|---|
| 911 | printf("You must set ISMROOT env var first. Ex. export ISMROOT=/home/joe\n"); |
|---|
| 912 | exit(1); |
|---|
| 913 | } |
|---|
| 914 | |
|---|
| 915 | printf("Creating db and setting permissions, installing data from %sunxsVZ...\n\n", |
|---|
| 916 | cISMROOT); |
|---|
| 917 | |
|---|
| 918 | //connect as root to master db |
|---|
| 919 | mySQLRootConnect(cPasswd); |
|---|
| 920 | |
|---|
| 921 | //Create database |
|---|
| 922 | sprintf(gcQuery,"CREATE DATABASE %s",DBNAME); |
|---|
| 923 | mysql_query(&gMysql,gcQuery); |
|---|
| 924 | if(mysql_errno(&gMysql)) |
|---|
| 925 | { |
|---|
| 926 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 927 | exit(1); |
|---|
| 928 | } |
|---|
| 929 | |
|---|
| 930 | //Grant localaccess privileges. |
|---|
| 931 | sprintf(gcQuery,"GRANT ALL ON %s.* to %s@localhost IDENTIFIED BY '%s'", |
|---|
| 932 | DBNAME,DBLOGIN,DBPASSWD); |
|---|
| 933 | mysql_query(&gMysql,gcQuery); |
|---|
| 934 | if(mysql_errno(&gMysql)) |
|---|
| 935 | { |
|---|
| 936 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 937 | exit(1); |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| 940 | //Change to mysqlbind db. Then initialize some tables with needed data |
|---|
| 941 | sprintf(gcQuery,"USE %s",DBNAME); |
|---|
| 942 | mysql_query(&gMysql,gcQuery); |
|---|
| 943 | if(mysql_errno(&gMysql)) |
|---|
| 944 | { |
|---|
| 945 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 946 | exit(1); |
|---|
| 947 | } |
|---|
| 948 | |
|---|
| 949 | |
|---|
| 950 | //debug only |
|---|
| 951 | //Create tables and install default data |
|---|
| 952 | //Standard RAD3 required tables |
|---|
| 953 | CreatetAuthorize(); |
|---|
| 954 | CreatetClient(); |
|---|
| 955 | CreatetConfig(); |
|---|
| 956 | CreatetConfiguration(); |
|---|
| 957 | CreatetContainer(); |
|---|
| 958 | CreatetDatacenter(); |
|---|
| 959 | CreatetGlossary(); |
|---|
| 960 | CreatetGroup(); |
|---|
| 961 | CreatetGroupGlue(); |
|---|
| 962 | CreatetGroupType(); |
|---|
| 963 | CreatetIP(); |
|---|
| 964 | CreatetJob(); |
|---|
| 965 | CreatetJobStatus(); |
|---|
| 966 | CreatetLog(); |
|---|
| 967 | CreatetLogMonth(); |
|---|
| 968 | CreatetLogType(); |
|---|
| 969 | CreatetMonth(); |
|---|
| 970 | CreatetNameserver(); |
|---|
| 971 | CreatetNode(); |
|---|
| 972 | CreatetOSTemplate(); |
|---|
| 973 | CreatetProperty(); |
|---|
| 974 | CreatetSearchdomain(); |
|---|
| 975 | CreatetStatus(); |
|---|
| 976 | CreatetTemplate(); |
|---|
| 977 | CreatetTemplateSet(); |
|---|
| 978 | CreatetTemplateType(); |
|---|
| 979 | CreatetType(); |
|---|
| 980 | |
|---|
| 981 | //Unique uContainer values starting at 101 |
|---|
| 982 | sprintf(gcQuery,"INSERT INTO tContainer SET uContainer=100"); |
|---|
| 983 | mysql_query(&gMysql,gcQuery); |
|---|
| 984 | if(mysql_errno(&gMysql)) |
|---|
| 985 | { |
|---|
| 986 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 987 | exit(1); |
|---|
| 988 | } |
|---|
| 989 | sprintf(gcQuery,"DELETE FROM tContainer"); |
|---|
| 990 | mysql_query(&gMysql,gcQuery); |
|---|
| 991 | if(mysql_errno(&gMysql)) |
|---|
| 992 | { |
|---|
| 993 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 994 | exit(1); |
|---|
| 995 | } |
|---|
| 996 | |
|---|
| 997 | for(i=0;cInitTableList[i][0];i++) |
|---|
| 998 | { |
|---|
| 999 | sprintf(gcQuery,"LOAD DATA LOCAL INFILE '%s/unxsVZ/data/%s.txt' REPLACE INTO TABLE %s", |
|---|
| 1000 | cISMROOT,cInitTableList[i],cInitTableList[i]); |
|---|
| 1001 | mysql_query(&gMysql,gcQuery); |
|---|
| 1002 | if(mysql_errno(&gMysql)) |
|---|
| 1003 | { |
|---|
| 1004 | printf("%s.\nAttempting to drop database for re-initialize...\n",mysql_error(&gMysql)); |
|---|
| 1005 | |
|---|
| 1006 | sprintf(gcQuery,"DROP DATABASE %s",DBNAME); |
|---|
| 1007 | mysql_query(&gMysql,gcQuery); |
|---|
| 1008 | if(mysql_errno(&gMysql)) |
|---|
| 1009 | { |
|---|
| 1010 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1011 | exit(1); |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | exit(1); |
|---|
| 1015 | } |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | printf("Done\n"); |
|---|
| 1019 | |
|---|
| 1020 | }//void Initialize(void) |
|---|
| 1021 | |
|---|
| 1022 | |
|---|
| 1023 | //No need for redundancy here like TextConnectDb() |
|---|
| 1024 | void mySQLRootConnect(char *cPasswd) |
|---|
| 1025 | { |
|---|
| 1026 | mysql_init(&gMysql); |
|---|
| 1027 | if (!mysql_real_connect(&gMysql,NULL,"root",cPasswd,"mysql",0,NULL,0)) |
|---|
| 1028 | { |
|---|
| 1029 | printf("Database server unavailable\n"); |
|---|
| 1030 | exit(1); |
|---|
| 1031 | } |
|---|
| 1032 | }//void mySQLRootConnect(void) |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | void UpdateSchema(void) |
|---|
| 1036 | { |
|---|
| 1037 | MYSQL_RES *res; |
|---|
| 1038 | MYSQL_ROW field; |
|---|
| 1039 | unsigned uVeth=0; |
|---|
| 1040 | unsigned uBackupDate=0; |
|---|
| 1041 | unsigned uSource=0; |
|---|
| 1042 | |
|---|
| 1043 | unsigned uIPDatacenter=0; |
|---|
| 1044 | unsigned uIPComment=0; |
|---|
| 1045 | unsigned uOSTemplateDatacenter=0; |
|---|
| 1046 | unsigned uConfigDatacenter=0; |
|---|
| 1047 | unsigned uNameserverDatacenter=0; |
|---|
| 1048 | unsigned uSearchdomainDatacenter=0; |
|---|
| 1049 | unsigned uGroupGlueIP=0; |
|---|
| 1050 | |
|---|
| 1051 | unsigned uIncorrectSource=0; |
|---|
| 1052 | unsigned uIncorrectVeth=0; |
|---|
| 1053 | unsigned uSourceIndex=0; |
|---|
| 1054 | unsigned uPropertyNameIndex=0; |
|---|
| 1055 | unsigned uJobStatusIndex=0; |
|---|
| 1056 | unsigned uJobNodeIndex=0; |
|---|
| 1057 | unsigned uJobDatacenterIndex=0; |
|---|
| 1058 | unsigned uJobContainerIndex=0; |
|---|
| 1059 | unsigned uTemplateLabelIndex=0; |
|---|
| 1060 | unsigned uGlossaryLabelIndex=0; |
|---|
| 1061 | |
|---|
| 1062 | printf("UpdateSchema(): Start\n"); |
|---|
| 1063 | |
|---|
| 1064 | if(TextConnectDb()) |
|---|
| 1065 | exit(1); |
|---|
| 1066 | |
|---|
| 1067 | //Take note if what we need to change/add |
|---|
| 1068 | //This is based on expanded and incorrect schema of previous releases. Yes this sucks. |
|---|
| 1069 | sprintf(gcQuery,"SHOW COLUMNS IN tContainer"); |
|---|
| 1070 | mysql_query(&gMysql,gcQuery); |
|---|
| 1071 | if(mysql_errno(&gMysql)) |
|---|
| 1072 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1073 | mysql_query(&gMysql,gcQuery); |
|---|
| 1074 | res=mysql_store_result(&gMysql); |
|---|
| 1075 | while((field=mysql_fetch_row(res))) |
|---|
| 1076 | { |
|---|
| 1077 | if(!strcmp(field[0],"uVeth")) |
|---|
| 1078 | { |
|---|
| 1079 | uVeth=1; |
|---|
| 1080 | if(!strcmp(field[2],"YES")) |
|---|
| 1081 | uIncorrectVeth=1; |
|---|
| 1082 | } |
|---|
| 1083 | if(!strcmp(field[0],"uSource")) |
|---|
| 1084 | { |
|---|
| 1085 | uSource=1; |
|---|
| 1086 | if(!strcmp(field[2],"YES")) |
|---|
| 1087 | uIncorrectSource=1; |
|---|
| 1088 | } |
|---|
| 1089 | if(!strcmp(field[0],"uBackupDate")) |
|---|
| 1090 | { |
|---|
| 1091 | uBackupDate=1; |
|---|
| 1092 | } |
|---|
| 1093 | } |
|---|
| 1094 | mysql_free_result(res); |
|---|
| 1095 | |
|---|
| 1096 | sprintf(gcQuery,"SHOW COLUMNS IN tIP"); |
|---|
| 1097 | mysql_query(&gMysql,gcQuery); |
|---|
| 1098 | if(mysql_errno(&gMysql)) |
|---|
| 1099 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1100 | mysql_query(&gMysql,gcQuery); |
|---|
| 1101 | res=mysql_store_result(&gMysql); |
|---|
| 1102 | while((field=mysql_fetch_row(res))) |
|---|
| 1103 | { |
|---|
| 1104 | if(!strcmp(field[0],"uDatacenter")) |
|---|
| 1105 | uIPDatacenter=1; |
|---|
| 1106 | if(!strcmp(field[0],"cComment")) |
|---|
| 1107 | uIPComment=1; |
|---|
| 1108 | } |
|---|
| 1109 | mysql_free_result(res); |
|---|
| 1110 | |
|---|
| 1111 | sprintf(gcQuery,"SHOW COLUMNS IN tOSTemplate"); |
|---|
| 1112 | mysql_query(&gMysql,gcQuery); |
|---|
| 1113 | if(mysql_errno(&gMysql)) |
|---|
| 1114 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1115 | mysql_query(&gMysql,gcQuery); |
|---|
| 1116 | res=mysql_store_result(&gMysql); |
|---|
| 1117 | while((field=mysql_fetch_row(res))) |
|---|
| 1118 | { |
|---|
| 1119 | if(!strcmp(field[0],"uDatacenter")) |
|---|
| 1120 | uOSTemplateDatacenter=1; |
|---|
| 1121 | } |
|---|
| 1122 | mysql_free_result(res); |
|---|
| 1123 | |
|---|
| 1124 | sprintf(gcQuery,"SHOW COLUMNS IN tConfig"); |
|---|
| 1125 | mysql_query(&gMysql,gcQuery); |
|---|
| 1126 | if(mysql_errno(&gMysql)) |
|---|
| 1127 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1128 | mysql_query(&gMysql,gcQuery); |
|---|
| 1129 | res=mysql_store_result(&gMysql); |
|---|
| 1130 | while((field=mysql_fetch_row(res))) |
|---|
| 1131 | { |
|---|
| 1132 | if(!strcmp(field[0],"uDatacenter")) |
|---|
| 1133 | uConfigDatacenter=1; |
|---|
| 1134 | } |
|---|
| 1135 | mysql_free_result(res); |
|---|
| 1136 | |
|---|
| 1137 | sprintf(gcQuery,"SHOW COLUMNS IN tNameserver"); |
|---|
| 1138 | mysql_query(&gMysql,gcQuery); |
|---|
| 1139 | if(mysql_errno(&gMysql)) |
|---|
| 1140 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1141 | mysql_query(&gMysql,gcQuery); |
|---|
| 1142 | res=mysql_store_result(&gMysql); |
|---|
| 1143 | while((field=mysql_fetch_row(res))) |
|---|
| 1144 | { |
|---|
| 1145 | if(!strcmp(field[0],"uDatacenter")) |
|---|
| 1146 | uNameserverDatacenter=1; |
|---|
| 1147 | } |
|---|
| 1148 | mysql_free_result(res); |
|---|
| 1149 | |
|---|
| 1150 | sprintf(gcQuery,"SHOW COLUMNS IN tSearchdomain"); |
|---|
| 1151 | mysql_query(&gMysql,gcQuery); |
|---|
| 1152 | if(mysql_errno(&gMysql)) |
|---|
| 1153 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1154 | mysql_query(&gMysql,gcQuery); |
|---|
| 1155 | res=mysql_store_result(&gMysql); |
|---|
| 1156 | while((field=mysql_fetch_row(res))) |
|---|
| 1157 | { |
|---|
| 1158 | if(!strcmp(field[0],"uDatacenter")) |
|---|
| 1159 | uSearchdomainDatacenter=1; |
|---|
| 1160 | } |
|---|
| 1161 | mysql_free_result(res); |
|---|
| 1162 | |
|---|
| 1163 | sprintf(gcQuery,"SHOW COLUMNS IN tGroupGlue"); |
|---|
| 1164 | mysql_query(&gMysql,gcQuery); |
|---|
| 1165 | if(mysql_errno(&gMysql)) |
|---|
| 1166 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1167 | mysql_query(&gMysql,gcQuery); |
|---|
| 1168 | res=mysql_store_result(&gMysql); |
|---|
| 1169 | while((field=mysql_fetch_row(res))) |
|---|
| 1170 | { |
|---|
| 1171 | if(!strcmp(field[0],"uIP")) |
|---|
| 1172 | uGroupGlueIP=1; |
|---|
| 1173 | } |
|---|
| 1174 | mysql_free_result(res); |
|---|
| 1175 | |
|---|
| 1176 | sprintf(gcQuery,"SHOW INDEX IN tContainer"); |
|---|
| 1177 | mysql_query(&gMysql,gcQuery); |
|---|
| 1178 | if(mysql_errno(&gMysql)) |
|---|
| 1179 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1180 | mysql_query(&gMysql,gcQuery); |
|---|
| 1181 | res=mysql_store_result(&gMysql); |
|---|
| 1182 | while((field=mysql_fetch_row(res))) |
|---|
| 1183 | { |
|---|
| 1184 | if(!strcmp(field[2],"uSource")) uSourceIndex=1; |
|---|
| 1185 | } |
|---|
| 1186 | mysql_free_result(res); |
|---|
| 1187 | |
|---|
| 1188 | sprintf(gcQuery,"SHOW INDEX IN tProperty"); |
|---|
| 1189 | mysql_query(&gMysql,gcQuery); |
|---|
| 1190 | if(mysql_errno(&gMysql)) |
|---|
| 1191 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1192 | mysql_query(&gMysql,gcQuery); |
|---|
| 1193 | res=mysql_store_result(&gMysql); |
|---|
| 1194 | while((field=mysql_fetch_row(res))) |
|---|
| 1195 | { |
|---|
| 1196 | if(!strcmp(field[2],"cName")) uPropertyNameIndex=1; |
|---|
| 1197 | } |
|---|
| 1198 | mysql_free_result(res); |
|---|
| 1199 | |
|---|
| 1200 | sprintf(gcQuery,"SHOW INDEX IN tJob"); |
|---|
| 1201 | mysql_query(&gMysql,gcQuery); |
|---|
| 1202 | if(mysql_errno(&gMysql)) |
|---|
| 1203 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1204 | mysql_query(&gMysql,gcQuery); |
|---|
| 1205 | res=mysql_store_result(&gMysql); |
|---|
| 1206 | while((field=mysql_fetch_row(res))) |
|---|
| 1207 | { |
|---|
| 1208 | if(!strcmp(field[2],"uJobStatus")) uJobStatusIndex=1; |
|---|
| 1209 | else if(!strcmp(field[2],"uContainer")) uJobContainerIndex=1; |
|---|
| 1210 | else if(!strcmp(field[2],"uNode")) uJobNodeIndex=1; |
|---|
| 1211 | else if(!strcmp(field[2],"uDatacenter")) uJobDatacenterIndex=1; |
|---|
| 1212 | } |
|---|
| 1213 | mysql_free_result(res); |
|---|
| 1214 | |
|---|
| 1215 | sprintf(gcQuery,"SHOW INDEX IN tTemplate"); |
|---|
| 1216 | mysql_query(&gMysql,gcQuery); |
|---|
| 1217 | if(mysql_errno(&gMysql)) |
|---|
| 1218 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1219 | mysql_query(&gMysql,gcQuery); |
|---|
| 1220 | res=mysql_store_result(&gMysql); |
|---|
| 1221 | while((field=mysql_fetch_row(res))) |
|---|
| 1222 | { |
|---|
| 1223 | if(!strcmp(field[2],"cLabel")) uTemplateLabelIndex=1; |
|---|
| 1224 | } |
|---|
| 1225 | mysql_free_result(res); |
|---|
| 1226 | |
|---|
| 1227 | sprintf(gcQuery,"SHOW INDEX IN tGlossary"); |
|---|
| 1228 | mysql_query(&gMysql,gcQuery); |
|---|
| 1229 | if(mysql_errno(&gMysql)) |
|---|
| 1230 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1231 | mysql_query(&gMysql,gcQuery); |
|---|
| 1232 | res=mysql_store_result(&gMysql); |
|---|
| 1233 | while((field=mysql_fetch_row(res))) |
|---|
| 1234 | { |
|---|
| 1235 | if(!strcmp(field[2],"cLabel")) uGlossaryLabelIndex=1; |
|---|
| 1236 | } |
|---|
| 1237 | mysql_free_result(res); |
|---|
| 1238 | |
|---|
| 1239 | if(uIncorrectVeth) |
|---|
| 1240 | { |
|---|
| 1241 | sprintf(gcQuery,"ALTER TABLE tContainer MODIFY uVeth INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1242 | mysql_query(&gMysql,gcQuery); |
|---|
| 1243 | if(mysql_errno(&gMysql)) |
|---|
| 1244 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1245 | else |
|---|
| 1246 | printf("Corrected uVeth of tContainer\n"); |
|---|
| 1247 | } |
|---|
| 1248 | if(!uVeth) |
|---|
| 1249 | { |
|---|
| 1250 | sprintf(gcQuery,"ALTER TABLE tContainer ADD uVeth INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1251 | mysql_query(&gMysql,gcQuery); |
|---|
| 1252 | if(mysql_errno(&gMysql)) |
|---|
| 1253 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1254 | else |
|---|
| 1255 | printf("Added uVeth to tContainer\n"); |
|---|
| 1256 | } |
|---|
| 1257 | if(!uBackupDate) |
|---|
| 1258 | { |
|---|
| 1259 | sprintf(gcQuery,"ALTER TABLE tContainer ADD uBackupDate INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1260 | mysql_query(&gMysql,gcQuery); |
|---|
| 1261 | if(mysql_errno(&gMysql)) |
|---|
| 1262 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1263 | else |
|---|
| 1264 | printf("Added uBackupDate to tContainer\n"); |
|---|
| 1265 | } |
|---|
| 1266 | |
|---|
| 1267 | sprintf(gcQuery,"SELECT uStatus FROM tStatus WHERE uStatus=81"); |
|---|
| 1268 | mysql_query(&gMysql,gcQuery); |
|---|
| 1269 | if(mysql_errno(&gMysql)) |
|---|
| 1270 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1271 | mysql_query(&gMysql,gcQuery); |
|---|
| 1272 | res=mysql_store_result(&gMysql); |
|---|
| 1273 | if(mysql_num_rows(res)==0) |
|---|
| 1274 | { |
|---|
| 1275 | sprintf(gcQuery,"INSERT INTO tStatus SET uStatus=81,cLabel='Awaiting Clone'," |
|---|
| 1276 | "uCreatedBy=1,uOwner=1,uCreatedDate=UNIX_TIMESTAMP(NOW())"); |
|---|
| 1277 | mysql_query(&gMysql,gcQuery); |
|---|
| 1278 | if(mysql_errno(&gMysql)) |
|---|
| 1279 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1280 | else |
|---|
| 1281 | printf("Inserted uStatus=81 into tStatus\n"); |
|---|
| 1282 | } |
|---|
| 1283 | mysql_free_result(res); |
|---|
| 1284 | |
|---|
| 1285 | sprintf(gcQuery,"SELECT uStatus FROM tStatus WHERE uStatus=91"); |
|---|
| 1286 | mysql_query(&gMysql,gcQuery); |
|---|
| 1287 | if(mysql_errno(&gMysql)) |
|---|
| 1288 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1289 | mysql_query(&gMysql,gcQuery); |
|---|
| 1290 | res=mysql_store_result(&gMysql); |
|---|
| 1291 | if(mysql_num_rows(res)==0) |
|---|
| 1292 | { |
|---|
| 1293 | sprintf(gcQuery,"INSERT INTO tStatus SET uStatus=91,cLabel='Awaiting Failover'," |
|---|
| 1294 | "uCreatedBy=1,uOwner=1,uCreatedDate=UNIX_TIMESTAMP(NOW())"); |
|---|
| 1295 | mysql_query(&gMysql,gcQuery); |
|---|
| 1296 | if(mysql_errno(&gMysql)) |
|---|
| 1297 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1298 | else |
|---|
| 1299 | printf("Inserted uStatus=91 into tStatus\n"); |
|---|
| 1300 | } |
|---|
| 1301 | mysql_free_result(res); |
|---|
| 1302 | |
|---|
| 1303 | //Not important if repeated |
|---|
| 1304 | sprintf(gcQuery,"UPDATE tConfiguration SET cLabel='cSSHOptions' WHERE cLabel='cSSLOptions'"); |
|---|
| 1305 | mysql_query(&gMysql,gcQuery); |
|---|
| 1306 | if(mysql_errno(&gMysql)) |
|---|
| 1307 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1308 | |
|---|
| 1309 | //Not important if repeated |
|---|
| 1310 | sprintf(gcQuery,"ALTER TABLE tOSTemplate MODIFY cLabel VARCHAR(100) NOT NULL DEFAULT ''"); |
|---|
| 1311 | mysql_query(&gMysql,gcQuery); |
|---|
| 1312 | if(mysql_errno(&gMysql)) |
|---|
| 1313 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1314 | |
|---|
| 1315 | if(uIncorrectSource) |
|---|
| 1316 | { |
|---|
| 1317 | sprintf(gcQuery,"ALTER TABLE tContainer MODIFY uSource INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1318 | mysql_query(&gMysql,gcQuery); |
|---|
| 1319 | if(mysql_errno(&gMysql)) |
|---|
| 1320 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1321 | else |
|---|
| 1322 | printf("Corrected uSource of tContainer\n"); |
|---|
| 1323 | |
|---|
| 1324 | sprintf(gcQuery,"ALTER TABLE tContainer DROP INDEX uSource"); |
|---|
| 1325 | mysql_query(&gMysql,gcQuery); |
|---|
| 1326 | if(mysql_errno(&gMysql)) |
|---|
| 1327 | { |
|---|
| 1328 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1329 | } |
|---|
| 1330 | else |
|---|
| 1331 | { |
|---|
| 1332 | printf("Dropped uSource index of tContainer\n"); |
|---|
| 1333 | uSourceIndex=0; |
|---|
| 1334 | } |
|---|
| 1335 | } |
|---|
| 1336 | |
|---|
| 1337 | if(!uSource) |
|---|
| 1338 | { |
|---|
| 1339 | sprintf(gcQuery,"ALTER TABLE tContainer ADD uSource INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1340 | mysql_query(&gMysql,gcQuery); |
|---|
| 1341 | if(mysql_errno(&gMysql)) |
|---|
| 1342 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1343 | else |
|---|
| 1344 | printf("Added uSource to tContainer\n"); |
|---|
| 1345 | } |
|---|
| 1346 | |
|---|
| 1347 | if(!uSourceIndex) |
|---|
| 1348 | { |
|---|
| 1349 | sprintf(gcQuery,"ALTER TABLE tContainer ADD INDEX (uSource)"); |
|---|
| 1350 | mysql_query(&gMysql,gcQuery); |
|---|
| 1351 | if(mysql_errno(&gMysql)) |
|---|
| 1352 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1353 | else |
|---|
| 1354 | printf("Added INDEX uSource tContainer\n"); |
|---|
| 1355 | } |
|---|
| 1356 | |
|---|
| 1357 | //alter table tProperty add index (cName) |
|---|
| 1358 | if(!uPropertyNameIndex) |
|---|
| 1359 | { |
|---|
| 1360 | sprintf(gcQuery,"ALTER TABLE tProperty ADD INDEX (cName)"); |
|---|
| 1361 | mysql_query(&gMysql,gcQuery); |
|---|
| 1362 | if(mysql_errno(&gMysql)) |
|---|
| 1363 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1364 | else |
|---|
| 1365 | printf("Added INDEX cName tProperty\n"); |
|---|
| 1366 | } |
|---|
| 1367 | |
|---|
| 1368 | |
|---|
| 1369 | //alter table tJob add index |
|---|
| 1370 | if(!uJobStatusIndex) |
|---|
| 1371 | { |
|---|
| 1372 | sprintf(gcQuery,"ALTER TABLE tJob ADD INDEX (uJobStatus)"); |
|---|
| 1373 | mysql_query(&gMysql,gcQuery); |
|---|
| 1374 | if(mysql_errno(&gMysql)) |
|---|
| 1375 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1376 | else |
|---|
| 1377 | printf("Added INDEX uJobStatus tJob\n"); |
|---|
| 1378 | } |
|---|
| 1379 | |
|---|
| 1380 | if(!uJobNodeIndex) |
|---|
| 1381 | { |
|---|
| 1382 | sprintf(gcQuery,"ALTER TABLE tJob ADD INDEX (uNode)"); |
|---|
| 1383 | mysql_query(&gMysql,gcQuery); |
|---|
| 1384 | if(mysql_errno(&gMysql)) |
|---|
| 1385 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1386 | else |
|---|
| 1387 | printf("Added INDEX uNode tJob\n"); |
|---|
| 1388 | } |
|---|
| 1389 | |
|---|
| 1390 | if(!uJobDatacenterIndex) |
|---|
| 1391 | { |
|---|
| 1392 | sprintf(gcQuery,"ALTER TABLE tJob ADD INDEX (uDatacenter)"); |
|---|
| 1393 | mysql_query(&gMysql,gcQuery); |
|---|
| 1394 | if(mysql_errno(&gMysql)) |
|---|
| 1395 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1396 | else |
|---|
| 1397 | printf("Added INDEX uDatacenter tJob\n"); |
|---|
| 1398 | } |
|---|
| 1399 | |
|---|
| 1400 | if(!uJobContainerIndex) |
|---|
| 1401 | { |
|---|
| 1402 | sprintf(gcQuery,"ALTER TABLE tJob ADD INDEX (uContainer)"); |
|---|
| 1403 | mysql_query(&gMysql,gcQuery); |
|---|
| 1404 | if(mysql_errno(&gMysql)) |
|---|
| 1405 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1406 | else |
|---|
| 1407 | printf("Added INDEX uContainer tJob\n"); |
|---|
| 1408 | } |
|---|
| 1409 | |
|---|
| 1410 | if(!uTemplateLabelIndex) |
|---|
| 1411 | { |
|---|
| 1412 | sprintf(gcQuery,"ALTER TABLE tTemplate ADD INDEX (cLabel)"); |
|---|
| 1413 | mysql_query(&gMysql,gcQuery); |
|---|
| 1414 | if(mysql_errno(&gMysql)) |
|---|
| 1415 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1416 | else |
|---|
| 1417 | printf("Added INDEX cLabel tTemplate\n"); |
|---|
| 1418 | } |
|---|
| 1419 | |
|---|
| 1420 | if(!uGlossaryLabelIndex) |
|---|
| 1421 | { |
|---|
| 1422 | sprintf(gcQuery,"ALTER TABLE tGlossary ADD INDEX (cLabel)"); |
|---|
| 1423 | mysql_query(&gMysql,gcQuery); |
|---|
| 1424 | if(mysql_errno(&gMysql)) |
|---|
| 1425 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1426 | else |
|---|
| 1427 | printf("Added INDEX cLabel tGlossary\n"); |
|---|
| 1428 | } |
|---|
| 1429 | |
|---|
| 1430 | if(!uIPDatacenter) |
|---|
| 1431 | { |
|---|
| 1432 | sprintf(gcQuery,"ALTER TABLE tIP ADD uDatacenter INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1433 | mysql_query(&gMysql,gcQuery); |
|---|
| 1434 | if(mysql_errno(&gMysql)) |
|---|
| 1435 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1436 | else |
|---|
| 1437 | printf("Added uDatacenter to tIP\n"); |
|---|
| 1438 | } |
|---|
| 1439 | |
|---|
| 1440 | if(!uIPComment) |
|---|
| 1441 | { |
|---|
| 1442 | sprintf(gcQuery,"ALTER TABLE tIP ADD cComment VARCHAR(255) NOT NULL DEFAULT ''"); |
|---|
| 1443 | mysql_query(&gMysql,gcQuery); |
|---|
| 1444 | if(mysql_errno(&gMysql)) |
|---|
| 1445 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1446 | else |
|---|
| 1447 | printf("Added cComment to tIP\n"); |
|---|
| 1448 | } |
|---|
| 1449 | |
|---|
| 1450 | if(!uOSTemplateDatacenter) |
|---|
| 1451 | { |
|---|
| 1452 | sprintf(gcQuery,"ALTER TABLE tOSTemplate ADD uDatacenter INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1453 | mysql_query(&gMysql,gcQuery); |
|---|
| 1454 | if(mysql_errno(&gMysql)) |
|---|
| 1455 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1456 | else |
|---|
| 1457 | printf("Added uDatacenter to tOSTemplate\n"); |
|---|
| 1458 | } |
|---|
| 1459 | |
|---|
| 1460 | if(!uConfigDatacenter) |
|---|
| 1461 | { |
|---|
| 1462 | sprintf(gcQuery,"ALTER TABLE tConfig ADD uDatacenter INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1463 | mysql_query(&gMysql,gcQuery); |
|---|
| 1464 | if(mysql_errno(&gMysql)) |
|---|
| 1465 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1466 | else |
|---|
| 1467 | printf("Added uDatacenter to tConfig\n"); |
|---|
| 1468 | } |
|---|
| 1469 | |
|---|
| 1470 | if(!uNameserverDatacenter) |
|---|
| 1471 | { |
|---|
| 1472 | sprintf(gcQuery,"ALTER TABLE tNameserver ADD uDatacenter INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1473 | mysql_query(&gMysql,gcQuery); |
|---|
| 1474 | if(mysql_errno(&gMysql)) |
|---|
| 1475 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1476 | else |
|---|
| 1477 | printf("Added uDatacenter to tNameserver\n"); |
|---|
| 1478 | } |
|---|
| 1479 | |
|---|
| 1480 | if(!uSearchdomainDatacenter) |
|---|
| 1481 | { |
|---|
| 1482 | sprintf(gcQuery,"ALTER TABLE tSearchdomain ADD uDatacenter INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1483 | mysql_query(&gMysql,gcQuery); |
|---|
| 1484 | if(mysql_errno(&gMysql)) |
|---|
| 1485 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1486 | else |
|---|
| 1487 | printf("Added uDatacenter to tSearchdomain\n"); |
|---|
| 1488 | } |
|---|
| 1489 | |
|---|
| 1490 | if(!uGroupGlueIP) |
|---|
| 1491 | { |
|---|
| 1492 | sprintf(gcQuery,"ALTER TABLE tGroupGlue ADD uIP INT UNSIGNED NOT NULL DEFAULT 0"); |
|---|
| 1493 | mysql_query(&gMysql,gcQuery); |
|---|
| 1494 | if(mysql_errno(&gMysql)) |
|---|
| 1495 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1496 | else |
|---|
| 1497 | printf("Added uIP to tGroupGlue\n"); |
|---|
| 1498 | sprintf(gcQuery,"ALTER TABLE tGroupGlue ADD INDEX (uIP)"); |
|---|
| 1499 | mysql_query(&gMysql,gcQuery); |
|---|
| 1500 | if(mysql_errno(&gMysql)) |
|---|
| 1501 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1502 | else |
|---|
| 1503 | printf("Added INDEX uIP to tGroupGlue\n"); |
|---|
| 1504 | } |
|---|
| 1505 | |
|---|
| 1506 | //Please fix this TODO |
|---|
| 1507 | sprintf(gcQuery,"ALTER TABLE tJob MODIFY cRemoteMsg VARCHAR(64) NOT NULL DEFAULT ''"); |
|---|
| 1508 | mysql_query(&gMysql,gcQuery); |
|---|
| 1509 | if(mysql_errno(&gMysql)) |
|---|
| 1510 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1511 | else |
|---|
| 1512 | printf("Corrected cRemoteMsg of tJob\n"); |
|---|
| 1513 | |
|---|
| 1514 | |
|---|
| 1515 | printf("UpdateSchema(): End\n"); |
|---|
| 1516 | |
|---|
| 1517 | }//void UpdateSchema(void) |
|---|
| 1518 | |
|---|
| 1519 | void ImportTemplateFile(char *cTemplate, char *cFile, char *cTemplateSet, char *cTemplateType) |
|---|
| 1520 | { |
|---|
| 1521 | FILE *fp; |
|---|
| 1522 | unsigned uTemplate=0; |
|---|
| 1523 | unsigned uTemplateSet=0; |
|---|
| 1524 | unsigned uTemplateType=0; |
|---|
| 1525 | MYSQL_RES *mysqlRes; |
|---|
| 1526 | MYSQL_ROW mysqlField; |
|---|
| 1527 | char cBuffer[2048]={""}; |
|---|
| 1528 | |
|---|
| 1529 | printf("\nImportTemplateFile(): Start\n"); |
|---|
| 1530 | |
|---|
| 1531 | if(TextConnectDb()) |
|---|
| 1532 | exit(1); |
|---|
| 1533 | |
|---|
| 1534 | sprintf(gcQuery,"USE %s",DBNAME); |
|---|
| 1535 | mysql_query(&gMysql,gcQuery); |
|---|
| 1536 | if(mysql_errno(&gMysql)) |
|---|
| 1537 | { |
|---|
| 1538 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1539 | exit(1); |
|---|
| 1540 | } |
|---|
| 1541 | |
|---|
| 1542 | if((fp=fopen(cFile,"r"))==NULL) |
|---|
| 1543 | { |
|---|
| 1544 | printf("Could not open %s\n",cFile); |
|---|
| 1545 | exit(1); |
|---|
| 1546 | } |
|---|
| 1547 | |
|---|
| 1548 | //uTemplateSet |
|---|
| 1549 | sprintf(gcQuery,"SELECT uTemplateSet FROM tTemplateSet WHERE cLabel='%s'",cTemplateSet); |
|---|
| 1550 | mysql_query(&gMysql,gcQuery); |
|---|
| 1551 | if(mysql_errno(&gMysql)) |
|---|
| 1552 | { |
|---|
| 1553 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1554 | exit(1); |
|---|
| 1555 | } |
|---|
| 1556 | mysqlRes=mysql_store_result(&gMysql); |
|---|
| 1557 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 1558 | sscanf(mysqlField[0],"%u",&uTemplateSet); |
|---|
| 1559 | mysql_free_result(mysqlRes); |
|---|
| 1560 | |
|---|
| 1561 | if(!uTemplateSet) |
|---|
| 1562 | { |
|---|
| 1563 | printf("Could not find tTemplateSet.clabel=%s\n",cTemplateSet); |
|---|
| 1564 | exit(1); |
|---|
| 1565 | } |
|---|
| 1566 | |
|---|
| 1567 | //uTemplateType |
|---|
| 1568 | sprintf(gcQuery,"SELECT uTemplateType FROM tTemplateType WHERE cLabel='%s'",cTemplateType); |
|---|
| 1569 | mysql_query(&gMysql,gcQuery); |
|---|
| 1570 | if(mysql_errno(&gMysql)) |
|---|
| 1571 | { |
|---|
| 1572 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1573 | exit(1); |
|---|
| 1574 | } |
|---|
| 1575 | mysqlRes=mysql_store_result(&gMysql); |
|---|
| 1576 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 1577 | sscanf(mysqlField[0],"%u",&uTemplateType); |
|---|
| 1578 | mysql_free_result(mysqlRes); |
|---|
| 1579 | |
|---|
| 1580 | if(!uTemplateType) |
|---|
| 1581 | { |
|---|
| 1582 | printf("Could not find tTemplateType.clabel=%s\n",cTemplateSet); |
|---|
| 1583 | exit(1); |
|---|
| 1584 | } |
|---|
| 1585 | |
|---|
| 1586 | //uTemplate |
|---|
| 1587 | sprintf(gcQuery,"SELECT uTemplate FROM tTemplate WHERE cLabel='%s' AND uTemplateSet=%u AND uTemplateType=%u", |
|---|
| 1588 | cTemplate,uTemplateSet,uTemplateType); |
|---|
| 1589 | mysql_query(&gMysql,gcQuery); |
|---|
| 1590 | if(mysql_errno(&gMysql)) |
|---|
| 1591 | { |
|---|
| 1592 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1593 | exit(1); |
|---|
| 1594 | } |
|---|
| 1595 | mysqlRes=mysql_store_result(&gMysql); |
|---|
| 1596 | if((mysqlField=mysql_fetch_row(mysqlRes))) |
|---|
| 1597 | sscanf(mysqlField[0],"%u",&uTemplate); |
|---|
| 1598 | mysql_free_result(mysqlRes); |
|---|
| 1599 | |
|---|
| 1600 | |
|---|
| 1601 | if(uTemplate) |
|---|
| 1602 | { |
|---|
| 1603 | printf("Updating tTemplate for %s %s %s\n",cTemplate,cTemplateSet,cTemplateType); |
|---|
| 1604 | sprintf(cBuffer,"UPDATE tTemplate SET uModBy=1,uModDate=UNIX_TIMESTAMP(NOW()),cTemplate=''," |
|---|
| 1605 | "uTemplateSet=%u,uTemplateType=%u,uModBy=1 WHERE uTemplate=%u", |
|---|
| 1606 | uTemplateSet,uTemplateType,uTemplate); |
|---|
| 1607 | mysql_query(&gMysql,cBuffer); |
|---|
| 1608 | if(mysql_errno(&gMysql)) |
|---|
| 1609 | { |
|---|
| 1610 | printf("%s\n%.254s\n",mysql_error(&gMysql),cBuffer); |
|---|
| 1611 | exit(1); |
|---|
| 1612 | } |
|---|
| 1613 | } |
|---|
| 1614 | else |
|---|
| 1615 | { |
|---|
| 1616 | printf("Inserting new tTemplate for %s\n",cTemplate); |
|---|
| 1617 | sprintf(cBuffer,"INSERT INTO tTemplate SET uOwner=1,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())," |
|---|
| 1618 | "cLabel='%s',uTemplateSet=%u,uTemplateType=%u",cTemplate,uTemplateSet,uTemplateType); |
|---|
| 1619 | mysql_query(&gMysql,cBuffer); |
|---|
| 1620 | if(mysql_errno(&gMysql)) |
|---|
| 1621 | { |
|---|
| 1622 | printf("%s\n%.254s\n",mysql_error(&gMysql),cBuffer); |
|---|
| 1623 | exit(1); |
|---|
| 1624 | } |
|---|
| 1625 | uTemplate=mysql_insert_id(&gMysql); |
|---|
| 1626 | } |
|---|
| 1627 | |
|---|
| 1628 | while(fgets(gcQuery,1024,fp)!=NULL) |
|---|
| 1629 | { |
|---|
| 1630 | sprintf(cBuffer,"UPDATE tTemplate SET cTemplate=CONCAT(cTemplate,'%s') WHERE uTemplate=%u", |
|---|
| 1631 | TextAreaSave(gcQuery),uTemplate); |
|---|
| 1632 | mysql_query(&gMysql,cBuffer); |
|---|
| 1633 | if(mysql_errno(&gMysql)) |
|---|
| 1634 | { |
|---|
| 1635 | printf("%s\n%.254s\n",mysql_error(&gMysql),cBuffer); |
|---|
| 1636 | exit(1); |
|---|
| 1637 | } |
|---|
| 1638 | } |
|---|
| 1639 | fclose(fp); |
|---|
| 1640 | |
|---|
| 1641 | printf("\nDone\n"); |
|---|
| 1642 | |
|---|
| 1643 | }//void ImportTemplateFile() |
|---|
| 1644 | |
|---|
| 1645 | |
|---|
| 1646 | void CalledByAlias(int iArgc,char *cArgv[]) |
|---|
| 1647 | { |
|---|
| 1648 | if(strstr(cArgv[0],"unxsVZRSS.xml")) |
|---|
| 1649 | { |
|---|
| 1650 | MYSQL_RES *res; |
|---|
| 1651 | MYSQL_ROW field; |
|---|
| 1652 | char cRSSDate[200]; |
|---|
| 1653 | char cLinkStart[200]={""}; |
|---|
| 1654 | time_t tTime,luClock; |
|---|
| 1655 | struct tm *tmTime; |
|---|
| 1656 | char *cHTTP="http"; |
|---|
| 1657 | |
|---|
| 1658 | time(&luClock); |
|---|
| 1659 | tTime=time(NULL); |
|---|
| 1660 | tmTime=gmtime(&tTime); |
|---|
| 1661 | strftime(cRSSDate,sizeof(cRSSDate),"%a, %d %b %Y %T GMT",tmTime); |
|---|
| 1662 | |
|---|
| 1663 | if(getenv("HTTP_HOST")!=NULL) |
|---|
| 1664 | sprintf(gcHost,"%.99s",getenv("HTTP_HOST")); |
|---|
| 1665 | if(getenv("HTTPS")!=NULL) |
|---|
| 1666 | cHTTP="https"; |
|---|
| 1667 | |
|---|
| 1668 | //This is the standard place. With much parsing nonsense we can |
|---|
| 1669 | // do better by using env vars |
|---|
| 1670 | sprintf(cLinkStart,"%s://%s/cgi-bin/unxsVZ.cgi",cHTTP,gcHost); |
|---|
| 1671 | |
|---|
| 1672 | printf("Content-type: text/xml\n\n"); |
|---|
| 1673 | |
|---|
| 1674 | //Open xml |
|---|
| 1675 | printf("<?xml version='1.0' encoding='UTF-8'?>\n"); |
|---|
| 1676 | printf("<rss version='2.0'>\n"); |
|---|
| 1677 | printf("<channel>\n"); |
|---|
| 1678 | printf("<title>unxsVZ RSS tJob Errors</title>\n"); |
|---|
| 1679 | printf("<link>http://openisp.net/unxsVZ</link>\n"); |
|---|
| 1680 | printf("<description>unxsVZ tJob Errors</description>\n"); |
|---|
| 1681 | printf("<lastBuildDate>%.199s</lastBuildDate>\n",cRSSDate); |
|---|
| 1682 | printf("<generator>unxsVZ RSS Generator</generator>\n"); |
|---|
| 1683 | printf("<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n"); |
|---|
| 1684 | printf("<ttl>120</ttl>\n"); |
|---|
| 1685 | |
|---|
| 1686 | //Loop for each tJob error |
|---|
| 1687 | //Connect to local mySQL |
|---|
| 1688 | if(TextConnectDb()) |
|---|
| 1689 | exit(1); |
|---|
| 1690 | |
|---|
| 1691 | sprintf(gcQuery,"SELECT uJob,cServer,cJobName,uUser,cJobData FROM tJob WHERE uJobStatus=4");//4 is tJobStatus Done Error(s) |
|---|
| 1692 | mysql_query(&gMysql,gcQuery); |
|---|
| 1693 | if(mysql_errno(&gMysql)) |
|---|
| 1694 | { |
|---|
| 1695 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1696 | exit(1); |
|---|
| 1697 | } |
|---|
| 1698 | res=mysql_store_result(&gMysql); |
|---|
| 1699 | while((field=mysql_fetch_row(res))) |
|---|
| 1700 | { |
|---|
| 1701 | printf("\n<item>\n"); |
|---|
| 1702 | printf("<title>unxsVZ.tJob.uJob=%s</title>\n",field[0]); |
|---|
| 1703 | printf("<link>%s?gcFunction=tJob&uJob=%s</link>\n",cLinkStart, |
|---|
| 1704 | field[0]); |
|---|
| 1705 | printf("<description>cJobName=%s Server=%s uUser=%s\ncJobData=(%s)</description>\n",field[2],field[1],field[3],field[4]); |
|---|
| 1706 | printf("<guid isPermaLink='false'>%s-%lu</guid>\n",field[0],luClock); |
|---|
| 1707 | printf("<pubDate>%.199s</pubDate>\n",cRSSDate); |
|---|
| 1708 | printf("</item>\n"); |
|---|
| 1709 | |
|---|
| 1710 | } |
|---|
| 1711 | mysql_free_result(res); |
|---|
| 1712 | |
|---|
| 1713 | //Close xml |
|---|
| 1714 | printf("\n</channel>\n"); |
|---|
| 1715 | printf("</rss>\n"); |
|---|
| 1716 | mysql_close(&gMysql); |
|---|
| 1717 | exit(0); |
|---|
| 1718 | } |
|---|
| 1719 | else |
|---|
| 1720 | { |
|---|
| 1721 | printf("Content-type: text/plain\n\n"); |
|---|
| 1722 | printf("Called as unsupported alias %s\n",cArgv[0]); |
|---|
| 1723 | exit(0); |
|---|
| 1724 | } |
|---|
| 1725 | |
|---|
| 1726 | }//void CalledByAlias(int iArgc,char *cArgv[]) |
|---|
| 1727 | |
|---|
| 1728 | |
|---|
| 1729 | void ExtracttLog(char *cMonth, char *cYear, char *cPasswd, char *cTablePath) |
|---|
| 1730 | { |
|---|
| 1731 | char cTableName[33]={""}; |
|---|
| 1732 | char cNextMonth[4]={""}; |
|---|
| 1733 | char cNextYear[8]={""}; |
|---|
| 1734 | char cThisYear[8]={""}; |
|---|
| 1735 | char cThisMonth[4]={""}; |
|---|
| 1736 | long uStart=0; |
|---|
| 1737 | long uEnd=0; |
|---|
| 1738 | unsigned uRows=0; |
|---|
| 1739 | time_t clock; |
|---|
| 1740 | struct tm *structTime; |
|---|
| 1741 | struct stat info; |
|---|
| 1742 | |
|---|
| 1743 | time(&clock); |
|---|
| 1744 | structTime=localtime(&clock); |
|---|
| 1745 | strftime(cThisYear,8,"%Y",structTime); |
|---|
| 1746 | strftime(cThisMonth,4,"%b",structTime); |
|---|
| 1747 | |
|---|
| 1748 | printf("ExtracttLog() Start\n"); |
|---|
| 1749 | |
|---|
| 1750 | printf("cThisMonth:%s cThisYear:%s\n",cThisMonth,cThisYear); |
|---|
| 1751 | |
|---|
| 1752 | if(!strcmp(cThisMonth,cMonth) && !strcmp(cThisYear,cYear)) |
|---|
| 1753 | { |
|---|
| 1754 | fprintf(stderr,"Can't extract this months data!\n"); |
|---|
| 1755 | exit(1); |
|---|
| 1756 | } |
|---|
| 1757 | |
|---|
| 1758 | if(stat("/usr/bin/myisampack",&info) ) |
|---|
| 1759 | { |
|---|
| 1760 | fprintf(stderr,"/usr/bin/myisampack is not installed!\n"); |
|---|
| 1761 | exit(1); |
|---|
| 1762 | } |
|---|
| 1763 | |
|---|
| 1764 | if(stat("/usr/bin/myisamchk",&info)) |
|---|
| 1765 | { |
|---|
| 1766 | fprintf(stderr,"/usr/bin/myisamchk is not installed!\n"); |
|---|
| 1767 | exit(1); |
|---|
| 1768 | } |
|---|
| 1769 | |
|---|
| 1770 | |
|---|
| 1771 | mySQLRootConnect(cPasswd); |
|---|
| 1772 | mysql_query(&gMysql,"USE idns"); |
|---|
| 1773 | if(mysql_errno(&gMysql)) |
|---|
| 1774 | { |
|---|
| 1775 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1776 | exit(1); |
|---|
| 1777 | } |
|---|
| 1778 | sprintf(gcQuery,"INSERT INTO tLog SET cMessage='ExtracttLog() Start...'," |
|---|
| 1779 | "cServer='%s',uLogType=4,uOwner=1,uCreatedBy=1," |
|---|
| 1780 | "uCreatedDate=UNIX_TIMESTAMP(NOW())",gcHostname); |
|---|
| 1781 | mysql_query(&gMysql,gcQuery); |
|---|
| 1782 | if(mysql_errno(&gMysql)) |
|---|
| 1783 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1784 | |
|---|
| 1785 | sprintf(cTableName,"t%.3s%.7s",cMonth,cYear); |
|---|
| 1786 | |
|---|
| 1787 | sprintf(gcQuery,"1-%s-%s",cMonth,cYear); |
|---|
| 1788 | uStart=cDateToUnixTime(gcQuery); |
|---|
| 1789 | printf("Start: %s",ctime(&uStart)); |
|---|
| 1790 | |
|---|
| 1791 | if(uStart== -1 || !uStart) |
|---|
| 1792 | { |
|---|
| 1793 | fprintf(stderr,"Garbled month year input (uStart)\n"); |
|---|
| 1794 | exit(1); |
|---|
| 1795 | } |
|---|
| 1796 | |
|---|
| 1797 | NextMonthYear(cMonth,cYear,cNextMonth,cNextYear); |
|---|
| 1798 | //Debug only |
|---|
| 1799 | //printf("%s %s to %s %s\n",cMonth,cYear,cNextMonth,cNextYear); |
|---|
| 1800 | //exit(0); |
|---|
| 1801 | |
|---|
| 1802 | sprintf(gcQuery,"1-%s-%s",cNextMonth,cNextYear); |
|---|
| 1803 | uEnd=cDateToUnixTime(gcQuery); |
|---|
| 1804 | printf("End: %s",ctime(&uEnd)); |
|---|
| 1805 | if(uEnd== -1 || !uEnd) |
|---|
| 1806 | { |
|---|
| 1807 | fprintf(stderr,"Garbled month year input (uEnd)\n"); |
|---|
| 1808 | exit(1); |
|---|
| 1809 | } |
|---|
| 1810 | |
|---|
| 1811 | |
|---|
| 1812 | CreatetLogTable(cTableName); |
|---|
| 1813 | if(mysql_errno(&gMysql)) |
|---|
| 1814 | { |
|---|
| 1815 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1816 | exit(1); |
|---|
| 1817 | } |
|---|
| 1818 | |
|---|
| 1819 | printf("Clearing data from %s...\n",cTableName); |
|---|
| 1820 | sprintf(gcQuery,"DELETE FROM %s",cTableName); |
|---|
| 1821 | mysql_query(&gMysql,gcQuery); |
|---|
| 1822 | if(mysql_errno(&gMysql)) |
|---|
| 1823 | { |
|---|
| 1824 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1825 | exit(1); |
|---|
| 1826 | } |
|---|
| 1827 | |
|---|
| 1828 | printf("Getting data from tLog...\n"); |
|---|
| 1829 | sprintf(gcQuery,"INSERT %s (uLog,cLabel,uLogType,cHash,uPermLevel,uLoginClient,cLogin," |
|---|
| 1830 | "cHost,uTablePK,cTableName,uOwner,uCreatedBy,uCreatedDate,uModBy,uModDate)" |
|---|
| 1831 | " SELECT uLog,cLabel,uLogType,cHash,uPermLevel,uLoginClient,cLogin,cHost," |
|---|
| 1832 | "uTablePK,cTableName,uOwner,uCreatedBy,uCreatedDate,uModBy,uModDate" |
|---|
| 1833 | " FROM tLog WHERE uCreatedDate>=%lu AND uCreatedDate<%lu", |
|---|
| 1834 | cTableName,uStart,uEnd); |
|---|
| 1835 | mysql_query(&gMysql,gcQuery); |
|---|
| 1836 | if(mysql_errno(&gMysql)) |
|---|
| 1837 | { |
|---|
| 1838 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1839 | exit(1); |
|---|
| 1840 | } |
|---|
| 1841 | |
|---|
| 1842 | //If 0 records inserted delete from tMonth and exit now |
|---|
| 1843 | uRows=mysql_affected_rows(&gMysql); |
|---|
| 1844 | |
|---|
| 1845 | printf("Number of rows found and inserted: %u\n",uRows); |
|---|
| 1846 | |
|---|
| 1847 | if(uRows) |
|---|
| 1848 | { |
|---|
| 1849 | |
|---|
| 1850 | sprintf(gcQuery,"REPLACE tMonth SET cLabel='%s',uOwner=1,uCreatedBy=1," |
|---|
| 1851 | "uCreatedDate=UNIX_TIMESTAMP(NOW())",cTableName); |
|---|
| 1852 | mysql_query(&gMysql,gcQuery); |
|---|
| 1853 | if(mysql_errno(&gMysql)) |
|---|
| 1854 | { |
|---|
| 1855 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1856 | exit(1); |
|---|
| 1857 | } |
|---|
| 1858 | } |
|---|
| 1859 | else |
|---|
| 1860 | { |
|---|
| 1861 | sprintf(gcQuery,"DELETE FROM tMonth WHERE cLabel='%s'",cTableName); |
|---|
| 1862 | mysql_query(&gMysql,gcQuery); |
|---|
| 1863 | if(mysql_errno(&gMysql)) |
|---|
| 1864 | { |
|---|
| 1865 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1866 | exit(1); |
|---|
| 1867 | } |
|---|
| 1868 | |
|---|
| 1869 | sprintf(gcQuery,"DROP TABLE %s",cTableName); |
|---|
| 1870 | mysql_query(&gMysql,gcQuery); |
|---|
| 1871 | if(mysql_errno(&gMysql)) |
|---|
| 1872 | { |
|---|
| 1873 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1874 | exit(1); |
|---|
| 1875 | } |
|---|
| 1876 | |
|---|
| 1877 | |
|---|
| 1878 | printf("Exiting early no data to be archived\n"); |
|---|
| 1879 | exit(0); |
|---|
| 1880 | } |
|---|
| 1881 | |
|---|
| 1882 | sprintf(gcQuery,"/usr/bin/myisampack %s/%s",cTablePath,cTableName); |
|---|
| 1883 | if(system(gcQuery)) |
|---|
| 1884 | { |
|---|
| 1885 | fprintf(stderr,"Failed: %s\n",gcQuery); |
|---|
| 1886 | exit(1); |
|---|
| 1887 | } |
|---|
| 1888 | |
|---|
| 1889 | sprintf(gcQuery,"/usr/bin/myisamchk -rq %s/%s",cTablePath,cTableName); |
|---|
| 1890 | if(system(gcQuery)) |
|---|
| 1891 | { |
|---|
| 1892 | fprintf(stderr,"Failed: %s\n",gcQuery); |
|---|
| 1893 | exit(1); |
|---|
| 1894 | } |
|---|
| 1895 | |
|---|
| 1896 | printf("Flushing compressed RO table...\n"); |
|---|
| 1897 | sprintf(gcQuery,"FLUSH TABLE %s",cTableName); |
|---|
| 1898 | mysql_query(&gMysql,gcQuery); |
|---|
| 1899 | if(mysql_errno(&gMysql)) |
|---|
| 1900 | { |
|---|
| 1901 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1902 | exit(1); |
|---|
| 1903 | } |
|---|
| 1904 | |
|---|
| 1905 | printf("Removing records from tLog...\n"); |
|---|
| 1906 | sprintf(gcQuery,"DELETE QUICK FROM tLog WHERE uCreatedDate>=%lu AND uCreatedDate<%lu AND uLogType!=5", |
|---|
| 1907 | uStart,uEnd); |
|---|
| 1908 | mysql_query(&gMysql,gcQuery); |
|---|
| 1909 | if(mysql_errno(&gMysql)) |
|---|
| 1910 | { |
|---|
| 1911 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1912 | exit(1); |
|---|
| 1913 | } |
|---|
| 1914 | |
|---|
| 1915 | |
|---|
| 1916 | printf("Extracted and Archived. Table flushed: %s\n",cTableName); |
|---|
| 1917 | printf("ExtracttLog() End\n"); |
|---|
| 1918 | sprintf(gcQuery,"INSERT INTO tLog SET cMessage='ExtracttLog() End',cServer='%s',uLogType=4," |
|---|
| 1919 | "uOwner=1,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())",gcHostname); |
|---|
| 1920 | mysql_query(&gMysql,gcQuery); |
|---|
| 1921 | if(mysql_errno(&gMysql)) |
|---|
| 1922 | fprintf(stderr,"%s\n",mysql_error(&gMysql)); |
|---|
| 1923 | exit(0); |
|---|
| 1924 | |
|---|
| 1925 | }//void ExtracttLog(char *cMonth, char *cYear, char *cPasswd, char *cTablePath) |
|---|
| 1926 | |
|---|
| 1927 | |
|---|
| 1928 | time_t cDateToUnixTime(char *cDate) |
|---|
| 1929 | { |
|---|
| 1930 | struct tm locTime; |
|---|
| 1931 | time_t res; |
|---|
| 1932 | |
|---|
| 1933 | bzero(&locTime, sizeof(struct tm)); |
|---|
| 1934 | if(strchr(cDate,'-')) |
|---|
| 1935 | strptime(cDate,"%d-%b-%Y", &locTime); |
|---|
| 1936 | else if(strchr(cDate,'/')) |
|---|
| 1937 | strptime(cDate,"%d/%b/%Y", &locTime); |
|---|
| 1938 | else if(strchr(cDate,' ')) |
|---|
| 1939 | strptime(cDate,"%d %b %Y", &locTime); |
|---|
| 1940 | locTime.tm_sec = 0; |
|---|
| 1941 | locTime.tm_min = 0; |
|---|
| 1942 | locTime.tm_hour = 0; |
|---|
| 1943 | res = mktime(&locTime); |
|---|
| 1944 | return(res); |
|---|
| 1945 | }//time_t cDateToUnixTime(char *cDate) |
|---|
| 1946 | |
|---|
| 1947 | |
|---|
| 1948 | //Another schema dependent item |
|---|
| 1949 | void CreatetLogTable(char *cTableName) |
|---|
| 1950 | { |
|---|
| 1951 | sprintf(gcQuery,"CREATE TABLE IF NOT EXISTS %s ( uTablePK VARCHAR(32) NOT NULL DEFAULT '', cHost VARCHAR(32) NOT NULL DEFAULT '', uLoginClient INT UNSIGNED NOT NULL DEFAULT 0, cLogin VARCHAR(32) NOT NULL DEFAULT '', uPermLevel INT UNSIGNED NOT NULL DEFAULT 0, cTableName VARCHAR(32) NOT NULL DEFAULT '', uLog INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, cLabel VARCHAR(64) NOT NULL DEFAULT '', uOwner INT UNSIGNED NOT NULL DEFAULT 0,index (uOwner), uCreatedBy INT UNSIGNED NOT NULL DEFAULT 0, uCreatedDate INT UNSIGNED NOT NULL DEFAULT 0, uModBy INT UNSIGNED NOT NULL DEFAULT 0, uModDate INT UNSIGNED NOT NULL DEFAULT 0, cHash VARCHAR(32) NOT NULL DEFAULT '', uLogType INT UNSIGNED NOT NULL DEFAULT 0,index (uLogType) )",cTableName); |
|---|
| 1952 | mysql_query(&gMysql,gcQuery); |
|---|
| 1953 | if(mysql_errno(&gMysql)) |
|---|
| 1954 | { |
|---|
| 1955 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 1956 | exit(1); |
|---|
| 1957 | } |
|---|
| 1958 | }//CreatetLogTable() |
|---|
| 1959 | |
|---|
| 1960 | |
|---|
| 1961 | void NextMonthYear(char *cMonth,char *cYear,char *cNextMonth,char *cNextYear) |
|---|
| 1962 | { |
|---|
| 1963 | unsigned uYear=0; |
|---|
| 1964 | |
|---|
| 1965 | //Preset for all but Dec cases |
|---|
| 1966 | sprintf(cNextYear,"%.7s",cYear); |
|---|
| 1967 | sscanf(cNextYear,"%u",&uYear); |
|---|
| 1968 | |
|---|
| 1969 | if(!strcmp(cMonth,"Jan")) |
|---|
| 1970 | { |
|---|
| 1971 | strcpy(cNextMonth,"Feb"); |
|---|
| 1972 | } |
|---|
| 1973 | else if(!strcmp(cMonth,"Feb")) |
|---|
| 1974 | { |
|---|
| 1975 | strcpy(cNextMonth,"Mar"); |
|---|
| 1976 | } |
|---|
| 1977 | else if(!strcmp(cMonth,"Mar")) |
|---|
| 1978 | { |
|---|
| 1979 | strcpy(cNextMonth,"Apr"); |
|---|
| 1980 | } |
|---|
| 1981 | else if(!strcmp(cMonth,"Apr")) |
|---|
| 1982 | { |
|---|
| 1983 | strcpy(cNextMonth,"May"); |
|---|
| 1984 | } |
|---|
| 1985 | else if(!strcmp(cMonth,"May")) |
|---|
| 1986 | { |
|---|
| 1987 | strcpy(cNextMonth,"Jun"); |
|---|
| 1988 | } |
|---|
| 1989 | else if(!strcmp(cMonth,"Jun")) |
|---|
| 1990 | { |
|---|
| 1991 | strcpy(cNextMonth,"Jul"); |
|---|
| 1992 | } |
|---|
| 1993 | else if(!strcmp(cMonth,"Jul")) |
|---|
| 1994 | { |
|---|
| 1995 | strcpy(cNextMonth,"Aug"); |
|---|
| 1996 | } |
|---|
| 1997 | else if(!strcmp(cMonth,"Aug")) |
|---|
| 1998 | { |
|---|
| 1999 | strcpy(cNextMonth,"Sep"); |
|---|
| 2000 | } |
|---|
| 2001 | else if(!strcmp(cMonth,"Sep")) |
|---|
| 2002 | { |
|---|
| 2003 | strcpy(cNextMonth,"Oct"); |
|---|
| 2004 | } |
|---|
| 2005 | else if(!strcmp(cMonth,"Oct")) |
|---|
| 2006 | { |
|---|
| 2007 | strcpy(cNextMonth,"Nov"); |
|---|
| 2008 | } |
|---|
| 2009 | else if(!strcmp(cMonth,"Nov")) |
|---|
| 2010 | { |
|---|
| 2011 | strcpy(cNextMonth,"Dec"); |
|---|
| 2012 | } |
|---|
| 2013 | else if(!strcmp(cMonth,"Dec")) |
|---|
| 2014 | { |
|---|
| 2015 | strcpy(cNextMonth,"Jan"); |
|---|
| 2016 | uYear++; |
|---|
| 2017 | sprintf(cNextYear,"%u",uYear); |
|---|
| 2018 | } |
|---|
| 2019 | |
|---|
| 2020 | }//NextMonthYear() |
|---|
| 2021 | |
|---|
| 2022 | |
|---|
| 2023 | void TextError(const char *cError, unsigned uContinue) |
|---|
| 2024 | { |
|---|
| 2025 | printf("\nPlease report this unxsVZ fatal error ASAP:\n%s\n",cError); |
|---|
| 2026 | |
|---|
| 2027 | //Attempt to report error in tLog |
|---|
| 2028 | sprintf(gcQuery,"INSERT INTO tLog SET cLabel='TextError',uLogType=4,uPermLevel=%u,uLoginClient=%u," |
|---|
| 2029 | "cLogin='%s',cHost='%s',cMessage=\"%s\",cServer='%s',uOwner=1,uCreatedBy=%u," |
|---|
| 2030 | "uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2031 | guPermLevel,guLoginClient,gcUser,gcHost,cError,gcHostname,guLoginClient); |
|---|
| 2032 | mysql_query(&gMysql,gcQuery); |
|---|
| 2033 | if(mysql_errno(&gMysql)) |
|---|
| 2034 | printf("Another error occurred while attempting to log: %s\n", |
|---|
| 2035 | mysql_error(&gMysql)); |
|---|
| 2036 | if(!uContinue) exit(0); |
|---|
| 2037 | |
|---|
| 2038 | }//void TextError(const char *cError, unsigned uContinue) |
|---|
| 2039 | |
|---|
| 2040 | |
|---|
| 2041 | void GetConfiguration(const char *cName,char *cValue, |
|---|
| 2042 | unsigned uDatacenter, |
|---|
| 2043 | unsigned uNode, |
|---|
| 2044 | unsigned uContainer, |
|---|
| 2045 | unsigned uHtml) |
|---|
| 2046 | { |
|---|
| 2047 | MYSQL_RES *res; |
|---|
| 2048 | MYSQL_ROW field; |
|---|
| 2049 | |
|---|
| 2050 | char cQuery[1024]; |
|---|
| 2051 | char cExtra[100]={""}; |
|---|
| 2052 | |
|---|
| 2053 | sprintf(cQuery,"SELECT cValue FROM tConfiguration WHERE cLabel='%s'", |
|---|
| 2054 | cName); |
|---|
| 2055 | if(uDatacenter) |
|---|
| 2056 | { |
|---|
| 2057 | sprintf(cExtra," AND uDatacenter=%u",uDatacenter); |
|---|
| 2058 | strcat(cQuery,cExtra); |
|---|
| 2059 | } |
|---|
| 2060 | if(uNode) |
|---|
| 2061 | { |
|---|
| 2062 | sprintf(cExtra," AND uNode=%u",uNode); |
|---|
| 2063 | strcat(cQuery,cExtra); |
|---|
| 2064 | } |
|---|
| 2065 | if(uContainer) |
|---|
| 2066 | { |
|---|
| 2067 | sprintf(cExtra," AND uContainer=%u",uContainer); |
|---|
| 2068 | strcat(cQuery,cExtra); |
|---|
| 2069 | } |
|---|
| 2070 | mysql_query(&gMysql,cQuery); |
|---|
| 2071 | if(mysql_errno(&gMysql)) |
|---|
| 2072 | { |
|---|
| 2073 | if(uHtml) |
|---|
| 2074 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 2075 | else |
|---|
| 2076 | TextError(mysql_error(&gMysql),0); |
|---|
| 2077 | } |
|---|
| 2078 | res=mysql_store_result(&gMysql); |
|---|
| 2079 | if((field=mysql_fetch_row(res))) |
|---|
| 2080 | sprintf(cValue,"%.255s",field[0]); |
|---|
| 2081 | mysql_free_result(res); |
|---|
| 2082 | |
|---|
| 2083 | }//void GetConfiguration(...) |
|---|
| 2084 | |
|---|
| 2085 | |
|---|
| 2086 | unsigned HostnameContainerJob(unsigned uDatacenter,unsigned uNode,unsigned uContainer,char *cPrevHostname,unsigned uOwner,unsigned uLoginClient); |
|---|
| 2087 | void UpdateCloneHostnames(void) |
|---|
| 2088 | { |
|---|
| 2089 | MYSQL_RES *res; |
|---|
| 2090 | MYSQL_ROW field; |
|---|
| 2091 | |
|---|
| 2092 | printf("UpdateCloneHostnames(): Start\n"); |
|---|
| 2093 | |
|---|
| 2094 | if(TextConnectDb()) |
|---|
| 2095 | exit(1); |
|---|
| 2096 | |
|---|
| 2097 | sprintf(gcQuery,"SELECT cLabel,cHostname,uDatacenter,uNode,uContainer FROM tContainer WHERE uSource!=0 AND uStatus=81" |
|---|
| 2098 | " AND cLabel LIKE '%%-clone%%' AND cHostname LIKE '%%.clone%%'"); |
|---|
| 2099 | mysql_query(&gMysql,gcQuery); |
|---|
| 2100 | if(mysql_errno(&gMysql)) |
|---|
| 2101 | { |
|---|
| 2102 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2103 | mysql_close(&gMysql); |
|---|
| 2104 | exit(2); |
|---|
| 2105 | } |
|---|
| 2106 | res=mysql_store_result(&gMysql); |
|---|
| 2107 | while((field=mysql_fetch_row(res))) |
|---|
| 2108 | { |
|---|
| 2109 | char *cp=NULL; |
|---|
| 2110 | char cLabel[100]={""}; |
|---|
| 2111 | char cHostname[100]={""}; |
|---|
| 2112 | char cNewHostname[100]={""}; |
|---|
| 2113 | unsigned uDatacenter=0,uNode=0,uContainer=0,uCloneNumber=0; |
|---|
| 2114 | |
|---|
| 2115 | sscanf(field[2],"%u",&uDatacenter); |
|---|
| 2116 | sscanf(field[3],"%u",&uNode); |
|---|
| 2117 | sscanf(field[4],"%u",&uContainer); |
|---|
| 2118 | |
|---|
| 2119 | if(uDatacenter==0 || uNode==0 || uContainer==0 || field[0][0]==0 || field[1][0]==0) |
|---|
| 2120 | { |
|---|
| 2121 | printf("%s error\n",field[1]); |
|---|
| 2122 | continue; |
|---|
| 2123 | } |
|---|
| 2124 | |
|---|
| 2125 | sprintf(cLabel,"%.99s",field[0]); |
|---|
| 2126 | sprintf(cHostname,"%.99s",field[1]); |
|---|
| 2127 | |
|---|
| 2128 | if((cp=strstr(cLabel,"-clone"))) |
|---|
| 2129 | { |
|---|
| 2130 | *cp=0; |
|---|
| 2131 | } |
|---|
| 2132 | if((cp=strstr(cHostname,".clone"))) |
|---|
| 2133 | { |
|---|
| 2134 | *cp=0; |
|---|
| 2135 | sscanf(cp+1,"clone%u",&uCloneNumber); |
|---|
| 2136 | if(uCloneNumber==0) |
|---|
| 2137 | { |
|---|
| 2138 | printf("%s %s error\n",field[1],cp+1); |
|---|
| 2139 | continue; |
|---|
| 2140 | } |
|---|
| 2141 | if((cp=strchr(cHostname,'.'))) |
|---|
| 2142 | { |
|---|
| 2143 | *cp=0; |
|---|
| 2144 | sprintf(cNewHostname,"%s-clone%u.%s",cLabel,uCloneNumber,cp+1); |
|---|
| 2145 | } |
|---|
| 2146 | else |
|---|
| 2147 | { |
|---|
| 2148 | printf("%s . error\n",field[1]); |
|---|
| 2149 | continue; |
|---|
| 2150 | } |
|---|
| 2151 | } |
|---|
| 2152 | |
|---|
| 2153 | printf("%s %s %s %u\n",field[0],cNewHostname,field[1],uContainer); |
|---|
| 2154 | |
|---|
| 2155 | sprintf(gcQuery,"UPDATE tContainer SET cHostname='%s' WHERE uContainer=%u",cNewHostname,uContainer); |
|---|
| 2156 | mysql_query(&gMysql,gcQuery); |
|---|
| 2157 | if(mysql_errno(&gMysql)) |
|---|
| 2158 | { |
|---|
| 2159 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2160 | mysql_close(&gMysql); |
|---|
| 2161 | exit(2); |
|---|
| 2162 | } |
|---|
| 2163 | |
|---|
| 2164 | //HostnameContainerJob(uDatacenter,uNode,uContainer,field[1],1,1); |
|---|
| 2165 | } |
|---|
| 2166 | mysql_free_result(res); |
|---|
| 2167 | |
|---|
| 2168 | mysql_close(&gMysql); |
|---|
| 2169 | printf("UpdateCloneHostnames(): End\n"); |
|---|
| 2170 | |
|---|
| 2171 | }//void UpdateCloneHostnames(void) |
|---|
| 2172 | |
|---|
| 2173 | |
|---|
| 2174 | void RecoverMode(void) |
|---|
| 2175 | { |
|---|
| 2176 | MYSQL_RES *res; |
|---|
| 2177 | MYSQL_ROW field; |
|---|
| 2178 | char cHostname[100]; |
|---|
| 2179 | unsigned uNode=0,uDatacenter=0; |
|---|
| 2180 | |
|---|
| 2181 | printf("RecoverMode(): Start\n"); |
|---|
| 2182 | |
|---|
| 2183 | if(gethostname(cHostname,99)!=0) |
|---|
| 2184 | { |
|---|
| 2185 | printf("Could not determine cHostname\n"); |
|---|
| 2186 | exit(1); |
|---|
| 2187 | } |
|---|
| 2188 | |
|---|
| 2189 | if(TextConnectDb()) |
|---|
| 2190 | exit(1); |
|---|
| 2191 | |
|---|
| 2192 | //Get node and datacenter via hostname |
|---|
| 2193 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 2194 | mysql_query(&gMysql,gcQuery); |
|---|
| 2195 | if(mysql_errno(&gMysql)) |
|---|
| 2196 | { |
|---|
| 2197 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2198 | mysql_close(&gMysql); |
|---|
| 2199 | exit(2); |
|---|
| 2200 | } |
|---|
| 2201 | res=mysql_store_result(&gMysql); |
|---|
| 2202 | if((field=mysql_fetch_row(res))) |
|---|
| 2203 | { |
|---|
| 2204 | sscanf(field[0],"%u",&uNode); |
|---|
| 2205 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 2206 | } |
|---|
| 2207 | mysql_free_result(res); |
|---|
| 2208 | |
|---|
| 2209 | //FQDN vs short name of 2nd NIC mess |
|---|
| 2210 | if(!uNode) |
|---|
| 2211 | { |
|---|
| 2212 | char *cp; |
|---|
| 2213 | |
|---|
| 2214 | if((cp=strchr(cHostname,'.'))) |
|---|
| 2215 | *cp=0; |
|---|
| 2216 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 2217 | mysql_query(&gMysql,gcQuery); |
|---|
| 2218 | if(mysql_errno(&gMysql)) |
|---|
| 2219 | { |
|---|
| 2220 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2221 | mysql_close(&gMysql); |
|---|
| 2222 | exit(2); |
|---|
| 2223 | } |
|---|
| 2224 | res=mysql_store_result(&gMysql); |
|---|
| 2225 | if((field=mysql_fetch_row(res))) |
|---|
| 2226 | { |
|---|
| 2227 | sscanf(field[0],"%u",&uNode); |
|---|
| 2228 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 2229 | } |
|---|
| 2230 | mysql_free_result(res); |
|---|
| 2231 | } |
|---|
| 2232 | |
|---|
| 2233 | if(!uNode) |
|---|
| 2234 | { |
|---|
| 2235 | printf("Could not determine uNode\n"); |
|---|
| 2236 | mysql_close(&gMysql); |
|---|
| 2237 | exit(1); |
|---|
| 2238 | } |
|---|
| 2239 | |
|---|
| 2240 | //Take note if what we need to change/add |
|---|
| 2241 | //This is based on expanded and incorrect schema of previous releases. Yes this sucks. |
|---|
| 2242 | sprintf(gcQuery,"SELECT uContainer FROM tJob WHERE cJobName='FailoverFrom' AND uJobStatus=1 AND uJobDate<UNIX_TIMESTAMP(NOW())" |
|---|
| 2243 | " AND uDatacenter=%u AND uNode=%u",uDatacenter,uNode); |
|---|
| 2244 | mysql_query(&gMysql,gcQuery); |
|---|
| 2245 | if(mysql_errno(&gMysql)) |
|---|
| 2246 | { |
|---|
| 2247 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2248 | mysql_close(&gMysql); |
|---|
| 2249 | exit(1); |
|---|
| 2250 | } |
|---|
| 2251 | res=mysql_store_result(&gMysql); |
|---|
| 2252 | while((field=mysql_fetch_row(res))) |
|---|
| 2253 | { |
|---|
| 2254 | //Nothing yet. Old startup issue was fixed in jobqueue.c |
|---|
| 2255 | } |
|---|
| 2256 | mysql_free_result(res); |
|---|
| 2257 | |
|---|
| 2258 | mysql_close(&gMysql); |
|---|
| 2259 | printf("RecoverMode(): End\nNow you can start vz\n"); |
|---|
| 2260 | |
|---|
| 2261 | }//void RecoverMode(void) |
|---|
| 2262 | |
|---|
| 2263 | |
|---|
| 2264 | void ResetAllSyncPeriod(void) |
|---|
| 2265 | { |
|---|
| 2266 | MYSQL_RES *res; |
|---|
| 2267 | MYSQL_ROW field; |
|---|
| 2268 | char cHostname[100]; |
|---|
| 2269 | unsigned uNode=0,uDatacenter=0; |
|---|
| 2270 | |
|---|
| 2271 | printf("ResetAllSyncPeriod(): Start\n"); |
|---|
| 2272 | |
|---|
| 2273 | if(gethostname(cHostname,99)!=0) |
|---|
| 2274 | { |
|---|
| 2275 | printf("Could not determine cHostname\n"); |
|---|
| 2276 | exit(1); |
|---|
| 2277 | } |
|---|
| 2278 | |
|---|
| 2279 | if(TextConnectDb()) |
|---|
| 2280 | exit(1); |
|---|
| 2281 | |
|---|
| 2282 | //Get node and datacenter via hostname |
|---|
| 2283 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 2284 | mysql_query(&gMysql,gcQuery); |
|---|
| 2285 | if(mysql_errno(&gMysql)) |
|---|
| 2286 | { |
|---|
| 2287 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2288 | mysql_close(&gMysql); |
|---|
| 2289 | exit(2); |
|---|
| 2290 | } |
|---|
| 2291 | res=mysql_store_result(&gMysql); |
|---|
| 2292 | if((field=mysql_fetch_row(res))) |
|---|
| 2293 | { |
|---|
| 2294 | sscanf(field[0],"%u",&uNode); |
|---|
| 2295 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 2296 | } |
|---|
| 2297 | mysql_free_result(res); |
|---|
| 2298 | |
|---|
| 2299 | //FQDN vs short name of 2nd NIC mess |
|---|
| 2300 | if(!uNode) |
|---|
| 2301 | { |
|---|
| 2302 | char *cp; |
|---|
| 2303 | |
|---|
| 2304 | if((cp=strchr(cHostname,'.'))) |
|---|
| 2305 | *cp=0; |
|---|
| 2306 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 2307 | mysql_query(&gMysql,gcQuery); |
|---|
| 2308 | if(mysql_errno(&gMysql)) |
|---|
| 2309 | { |
|---|
| 2310 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2311 | mysql_close(&gMysql); |
|---|
| 2312 | exit(2); |
|---|
| 2313 | } |
|---|
| 2314 | res=mysql_store_result(&gMysql); |
|---|
| 2315 | if((field=mysql_fetch_row(res))) |
|---|
| 2316 | { |
|---|
| 2317 | sscanf(field[0],"%u",&uNode); |
|---|
| 2318 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 2319 | } |
|---|
| 2320 | mysql_free_result(res); |
|---|
| 2321 | } |
|---|
| 2322 | |
|---|
| 2323 | if(!uNode) |
|---|
| 2324 | { |
|---|
| 2325 | printf("Could not determine uNode\n"); |
|---|
| 2326 | mysql_close(&gMysql); |
|---|
| 2327 | exit(1); |
|---|
| 2328 | } |
|---|
| 2329 | |
|---|
| 2330 | //Take note if what we need to change/add |
|---|
| 2331 | //This is based on expanded and incorrect schema of previous releases. Yes this sucks. |
|---|
| 2332 | sprintf(gcQuery,"SELECT tProperty.uProperty FROM tContainer,tProperty" |
|---|
| 2333 | " WHERE tProperty.uKey=tContainer.uContainer AND tProperty.uType=3 AND tProperty.cName='cuSyncPeriod'" |
|---|
| 2334 | " AND tProperty.cValue='0'" |
|---|
| 2335 | " AND tContainer.uSource>0" |
|---|
| 2336 | " AND LOCATE('-clone',tContainer.cLabel)>0" |
|---|
| 2337 | " AND tContainer.uDatacenter=%u AND tContainer.uNode=%u",uDatacenter,uNode); |
|---|
| 2338 | mysql_query(&gMysql,gcQuery); |
|---|
| 2339 | if(mysql_errno(&gMysql)) |
|---|
| 2340 | { |
|---|
| 2341 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2342 | mysql_close(&gMysql); |
|---|
| 2343 | exit(1); |
|---|
| 2344 | } |
|---|
| 2345 | res=mysql_store_result(&gMysql); |
|---|
| 2346 | while((field=mysql_fetch_row(res))) |
|---|
| 2347 | { |
|---|
| 2348 | printf("%s\n",field[0]); |
|---|
| 2349 | sprintf(gcQuery,"UPDATE tProperty SET cValue='1200' WHERE uProperty=%s",field[0]); |
|---|
| 2350 | mysql_query(&gMysql,gcQuery); |
|---|
| 2351 | if(mysql_errno(&gMysql)) |
|---|
| 2352 | { |
|---|
| 2353 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2354 | mysql_close(&gMysql); |
|---|
| 2355 | exit(1); |
|---|
| 2356 | } |
|---|
| 2357 | } |
|---|
| 2358 | mysql_free_result(res); |
|---|
| 2359 | |
|---|
| 2360 | mysql_close(&gMysql); |
|---|
| 2361 | printf("ResetAllSyncPeriod(): End\n"); |
|---|
| 2362 | |
|---|
| 2363 | }//void ResetAllSyncPeriod(void) |
|---|
| 2364 | |
|---|
| 2365 | |
|---|
| 2366 | //Import remote datacenter containers from-to specified node, and set as status offline |
|---|
| 2367 | //Local tDatacenter and tNode must be ready. |
|---|
| 2368 | //Local tIP,tOSTemplate,tConfig,tNameserver,tSearchdomain,tProperty will be modified as needed. |
|---|
| 2369 | //Clone containers will not be imported. |
|---|
| 2370 | //Only import basic tContainer data |
|---|
| 2371 | void ImportRemoteDatacenter( |
|---|
| 2372 | const char *cLocalDatacenter, |
|---|
| 2373 | const char *cRemoteDatacenter, |
|---|
| 2374 | const char *cLocalNode, |
|---|
| 2375 | const char *cRemoteNode, |
|---|
| 2376 | const char *cHost, |
|---|
| 2377 | const char *cUser, |
|---|
| 2378 | const char *cPasswd, |
|---|
| 2379 | const char *cuOwner) |
|---|
| 2380 | { |
|---|
| 2381 | MYSQL_RES *res; |
|---|
| 2382 | MYSQL_ROW field; |
|---|
| 2383 | |
|---|
| 2384 | MYSQL gMysqlExt; |
|---|
| 2385 | MYSQL_RES *resExt; |
|---|
| 2386 | MYSQL_ROW fieldExt; |
|---|
| 2387 | |
|---|
| 2388 | //Same for all import ops |
|---|
| 2389 | unsigned uLocalDatacenter=0; |
|---|
| 2390 | unsigned uLocalNode=0; |
|---|
| 2391 | unsigned uRemoteDatacenter=0; |
|---|
| 2392 | unsigned uRemoteNode=0; |
|---|
| 2393 | unsigned uClient=0;//Same as c/uOwner |
|---|
| 2394 | unsigned uGroup=0; |
|---|
| 2395 | |
|---|
| 2396 | //Used in loop |
|---|
| 2397 | unsigned uIP; |
|---|
| 2398 | unsigned uOSTemplate; |
|---|
| 2399 | unsigned uConfig; |
|---|
| 2400 | unsigned uNameserver; |
|---|
| 2401 | unsigned uSearchdomain; |
|---|
| 2402 | unsigned uContainer; |
|---|
| 2403 | |
|---|
| 2404 | printf("ImportRemoteDatacenter(): Start\n"); |
|---|
| 2405 | |
|---|
| 2406 | if(TextConnectDb()) |
|---|
| 2407 | exit(1); |
|---|
| 2408 | |
|---|
| 2409 | mysql_init(&gMysqlExt); |
|---|
| 2410 | if(!mysql_real_connect(&gMysqlExt,cHost,cUser,cPasswd,"unxsvz",0,NULL,0)) |
|---|
| 2411 | { |
|---|
| 2412 | printf("Could not connect and/or authenticate to remote database.\n"); |
|---|
| 2413 | exit(1); |
|---|
| 2414 | } |
|---|
| 2415 | |
|---|
| 2416 | //Checks |
|---|
| 2417 | //1. tDatacenter check |
|---|
| 2418 | sprintf(gcQuery,"SELECT uDatacenter FROM tDatacenter WHERE cLabel='%s'",cLocalDatacenter); |
|---|
| 2419 | mysql_query(&gMysql,gcQuery); |
|---|
| 2420 | if(mysql_errno(&gMysql)) |
|---|
| 2421 | { |
|---|
| 2422 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2423 | exit(1); |
|---|
| 2424 | } |
|---|
| 2425 | mysql_query(&gMysql,gcQuery); |
|---|
| 2426 | res=mysql_store_result(&gMysql); |
|---|
| 2427 | if((field=mysql_fetch_row(res))) |
|---|
| 2428 | sscanf(field[0],"%u",&uLocalDatacenter); |
|---|
| 2429 | mysql_free_result(res); |
|---|
| 2430 | if(!uLocalDatacenter) |
|---|
| 2431 | { |
|---|
| 2432 | printf("Local tDatacenter.cLabel=%s does not exist\n",cLocalDatacenter); |
|---|
| 2433 | exit(1); |
|---|
| 2434 | } |
|---|
| 2435 | |
|---|
| 2436 | sprintf(gcQuery,"SELECT uDatacenter FROM tDatacenter WHERE cLabel='%s'",cRemoteDatacenter); |
|---|
| 2437 | mysql_query(&gMysqlExt,gcQuery); |
|---|
| 2438 | if(mysql_errno(&gMysqlExt)) |
|---|
| 2439 | { |
|---|
| 2440 | printf("%s\n",mysql_error(&gMysqlExt)); |
|---|
| 2441 | exit(1); |
|---|
| 2442 | } |
|---|
| 2443 | mysql_query(&gMysqlExt,gcQuery); |
|---|
| 2444 | resExt=mysql_store_result(&gMysqlExt); |
|---|
| 2445 | if((fieldExt=mysql_fetch_row(resExt))) |
|---|
| 2446 | sscanf(fieldExt[0],"%u",&uRemoteDatacenter); |
|---|
| 2447 | mysql_free_result(resExt); |
|---|
| 2448 | if(!uRemoteDatacenter) |
|---|
| 2449 | { |
|---|
| 2450 | printf("Remote tDatacenter.cLabel=%s does not exist\n",cRemoteDatacenter); |
|---|
| 2451 | exit(1); |
|---|
| 2452 | } |
|---|
| 2453 | |
|---|
| 2454 | //2. tNode check |
|---|
| 2455 | sprintf(gcQuery,"SELECT uNode FROM tNode WHERE cLabel='%s'",cLocalNode); |
|---|
| 2456 | mysql_query(&gMysql,gcQuery); |
|---|
| 2457 | if(mysql_errno(&gMysql)) |
|---|
| 2458 | { |
|---|
| 2459 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2460 | exit(1); |
|---|
| 2461 | } |
|---|
| 2462 | mysql_query(&gMysql,gcQuery); |
|---|
| 2463 | res=mysql_store_result(&gMysql); |
|---|
| 2464 | if((field=mysql_fetch_row(res))) |
|---|
| 2465 | sscanf(field[0],"%u",&uLocalNode); |
|---|
| 2466 | mysql_free_result(res); |
|---|
| 2467 | if(!uLocalNode) |
|---|
| 2468 | { |
|---|
| 2469 | printf("Local tNode.cLabel=%s does not exist\n",cLocalNode); |
|---|
| 2470 | exit(1); |
|---|
| 2471 | } |
|---|
| 2472 | |
|---|
| 2473 | sprintf(gcQuery,"SELECT uNode FROM tNode WHERE cLabel='%s'",cRemoteNode); |
|---|
| 2474 | mysql_query(&gMysqlExt,gcQuery); |
|---|
| 2475 | if(mysql_errno(&gMysqlExt)) |
|---|
| 2476 | { |
|---|
| 2477 | printf("%s\n",mysql_error(&gMysqlExt)); |
|---|
| 2478 | exit(1); |
|---|
| 2479 | } |
|---|
| 2480 | mysql_query(&gMysqlExt,gcQuery); |
|---|
| 2481 | resExt=mysql_store_result(&gMysqlExt); |
|---|
| 2482 | if((fieldExt=mysql_fetch_row(resExt))) |
|---|
| 2483 | sscanf(fieldExt[0],"%u",&uRemoteNode); |
|---|
| 2484 | mysql_free_result(resExt); |
|---|
| 2485 | if(!uRemoteNode) |
|---|
| 2486 | { |
|---|
| 2487 | printf("Remote tNode.cLabel=%s does not exist\n",cRemoteNode); |
|---|
| 2488 | exit(1); |
|---|
| 2489 | } |
|---|
| 2490 | |
|---|
| 2491 | //3. tClient check |
|---|
| 2492 | sprintf(gcQuery,"SELECT uClient FROM tClient WHERE uClient=%s",cuOwner); |
|---|
| 2493 | mysql_query(&gMysql,gcQuery); |
|---|
| 2494 | if(mysql_errno(&gMysql)) |
|---|
| 2495 | { |
|---|
| 2496 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2497 | exit(1); |
|---|
| 2498 | } |
|---|
| 2499 | mysql_query(&gMysql,gcQuery); |
|---|
| 2500 | res=mysql_store_result(&gMysql); |
|---|
| 2501 | if((field=mysql_fetch_row(res))) |
|---|
| 2502 | sscanf(field[0],"%u",&uClient); |
|---|
| 2503 | mysql_free_result(res); |
|---|
| 2504 | if(!uClient) |
|---|
| 2505 | { |
|---|
| 2506 | printf("Local tClient.uClient=%s does not exist\n",cuOwner); |
|---|
| 2507 | exit(1); |
|---|
| 2508 | } |
|---|
| 2509 | |
|---|
| 2510 | //4. Optional tGroup with same name as remote datacenter cLabel |
|---|
| 2511 | sprintf(gcQuery,"SELECT uGroup FROM tGroup WHERE cLabel='%s'",cRemoteDatacenter); |
|---|
| 2512 | mysql_query(&gMysql,gcQuery); |
|---|
| 2513 | if(mysql_errno(&gMysql)) |
|---|
| 2514 | { |
|---|
| 2515 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2516 | exit(1); |
|---|
| 2517 | } |
|---|
| 2518 | mysql_query(&gMysql,gcQuery); |
|---|
| 2519 | res=mysql_store_result(&gMysql); |
|---|
| 2520 | if((field=mysql_fetch_row(res))) |
|---|
| 2521 | sscanf(field[0],"%u",&uGroup); |
|---|
| 2522 | mysql_free_result(res); |
|---|
| 2523 | |
|---|
| 2524 | //Start importing |
|---|
| 2525 | //Only active (1) not clones |
|---|
| 2526 | sprintf(gcQuery,"SELECT tContainer.cLabel,tContainer.cHostname," |
|---|
| 2527 | " tIP.cLabel,tOSTemplate.cLabel,tConfig.cLabel,tNameserver.cLabel,tSearchdomain.cLabel" |
|---|
| 2528 | " FROM tContainer,tIP,tOSTemplate,tConfig,tNameserver,tSearchdomain" |
|---|
| 2529 | " WHERE tContainer.uIPv4=tIP.uIP AND tContainer.uOSTemplate=tOSTemplate.uOSTemplate" |
|---|
| 2530 | " AND tContainer.uConfig=tConfig.uConfig AND tContainer.uNameserver=tNameserver.uNameserver" |
|---|
| 2531 | " AND tContainer.uSearchdomain=tSearchdomain.uSearchdomain" |
|---|
| 2532 | " AND tContainer.uStatus=1 AND tContainer.uSource=0 AND tContainer.uDatacenter=%u AND tContainer.uNode=%u", |
|---|
| 2533 | uRemoteDatacenter,uRemoteNode); |
|---|
| 2534 | mysql_query(&gMysqlExt,gcQuery); |
|---|
| 2535 | if(mysql_errno(&gMysqlExt)) |
|---|
| 2536 | { |
|---|
| 2537 | printf("%s\n",mysql_error(&gMysqlExt)); |
|---|
| 2538 | exit(1); |
|---|
| 2539 | } |
|---|
| 2540 | mysql_query(&gMysqlExt,gcQuery); |
|---|
| 2541 | resExt=mysql_store_result(&gMysqlExt); |
|---|
| 2542 | while((fieldExt=mysql_fetch_row(resExt))) |
|---|
| 2543 | { |
|---|
| 2544 | uIP=0; |
|---|
| 2545 | uOSTemplate=0; |
|---|
| 2546 | uConfig=0; |
|---|
| 2547 | uNameserver=0; |
|---|
| 2548 | uSearchdomain=0; |
|---|
| 2549 | uContainer=0; |
|---|
| 2550 | |
|---|
| 2551 | //printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", |
|---|
| 2552 | // fieldExt[0],fieldExt[1],fieldExt[2],fieldExt[3],fieldExt[4],fieldExt[5],fieldExt[6]); |
|---|
| 2553 | |
|---|
| 2554 | //Do not add if we already have a local tContainer.cLabel |
|---|
| 2555 | sprintf(gcQuery,"SELECT uContainer FROM tContainer WHERE cLabel='%s'",fieldExt[0]); |
|---|
| 2556 | mysql_query(&gMysql,gcQuery); |
|---|
| 2557 | if(mysql_errno(&gMysql)) |
|---|
| 2558 | { |
|---|
| 2559 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2560 | exit(1); |
|---|
| 2561 | } |
|---|
| 2562 | mysql_query(&gMysql,gcQuery); |
|---|
| 2563 | res=mysql_store_result(&gMysql); |
|---|
| 2564 | if(mysql_num_rows(res)!=0) |
|---|
| 2565 | { |
|---|
| 2566 | printf("Local tContainer.cLabel=%s exists\n",fieldExt[0]); |
|---|
| 2567 | continue; |
|---|
| 2568 | } |
|---|
| 2569 | mysql_free_result(res); |
|---|
| 2570 | |
|---|
| 2571 | //tIP.cLabel [2] |
|---|
| 2572 | sprintf(gcQuery,"SELECT uIP FROM tIP WHERE cLabel='%s'",fieldExt[2]); |
|---|
| 2573 | mysql_query(&gMysql,gcQuery); |
|---|
| 2574 | if(mysql_errno(&gMysql)) |
|---|
| 2575 | { |
|---|
| 2576 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2577 | exit(1); |
|---|
| 2578 | } |
|---|
| 2579 | res=mysql_store_result(&gMysql); |
|---|
| 2580 | if(mysql_num_rows(res)==0) |
|---|
| 2581 | { |
|---|
| 2582 | sprintf(gcQuery,"INSERT INTO tIP SET cLabel='%s',uAvailable=0,uDatacenter=%u,uOwner=%u," |
|---|
| 2583 | "uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2584 | fieldExt[2],uLocalDatacenter,uClient); |
|---|
| 2585 | mysql_query(&gMysql,gcQuery); |
|---|
| 2586 | if(mysql_errno(&gMysql)) |
|---|
| 2587 | { |
|---|
| 2588 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2589 | exit(1); |
|---|
| 2590 | } |
|---|
| 2591 | uIP=mysql_insert_id(&gMysql); |
|---|
| 2592 | } |
|---|
| 2593 | else |
|---|
| 2594 | { |
|---|
| 2595 | if((field=mysql_fetch_row(res))) |
|---|
| 2596 | sscanf(field[0],"%u",&uIP); |
|---|
| 2597 | //printf("Local tIP.cLabel=%s exists. Not adding.\n",fieldExt[2]); |
|---|
| 2598 | if(!uIP) |
|---|
| 2599 | { |
|---|
| 2600 | printf("Unexpected uIP==0 error skip to next container\n"); |
|---|
| 2601 | continue; |
|---|
| 2602 | } |
|---|
| 2603 | } |
|---|
| 2604 | mysql_free_result(res); |
|---|
| 2605 | |
|---|
| 2606 | //debug only |
|---|
| 2607 | //printf("Post tIP import stop %s uClient=%u\n",fieldExt[2],uClient); |
|---|
| 2608 | //exit(0); |
|---|
| 2609 | |
|---|
| 2610 | //tOSTemplate.cLabel [3] |
|---|
| 2611 | sprintf(gcQuery,"SELECT uOSTemplate FROM tOSTemplate WHERE cLabel='%s'",fieldExt[3]); |
|---|
| 2612 | mysql_query(&gMysql,gcQuery); |
|---|
| 2613 | if(mysql_errno(&gMysql)) |
|---|
| 2614 | { |
|---|
| 2615 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2616 | exit(1); |
|---|
| 2617 | } |
|---|
| 2618 | res=mysql_store_result(&gMysql); |
|---|
| 2619 | if(mysql_num_rows(res)==0) |
|---|
| 2620 | { |
|---|
| 2621 | sprintf(gcQuery,"INSERT INTO tOSTemplate SET cLabel='%s',uOwner=%u," |
|---|
| 2622 | "uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2623 | fieldExt[3],uClient); |
|---|
| 2624 | mysql_query(&gMysql,gcQuery); |
|---|
| 2625 | if(mysql_errno(&gMysql)) |
|---|
| 2626 | { |
|---|
| 2627 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2628 | exit(1); |
|---|
| 2629 | } |
|---|
| 2630 | uOSTemplate=mysql_insert_id(&gMysql); |
|---|
| 2631 | //Enable for only this datacenter via tProperty tOSTemplate type = 8 |
|---|
| 2632 | sprintf(gcQuery,"INSERT INTO tProperty SET cName='cDatacenter',cValue='%s',uType=8,uKey=%u," |
|---|
| 2633 | " uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2634 | cLocalDatacenter,uOSTemplate,uClient); |
|---|
| 2635 | mysql_query(&gMysql,gcQuery); |
|---|
| 2636 | if(mysql_errno(&gMysql)) |
|---|
| 2637 | { |
|---|
| 2638 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2639 | exit(1); |
|---|
| 2640 | } |
|---|
| 2641 | } |
|---|
| 2642 | else |
|---|
| 2643 | { |
|---|
| 2644 | if((field=mysql_fetch_row(res))) |
|---|
| 2645 | sscanf(field[0],"%u",&uOSTemplate); |
|---|
| 2646 | //printf("Local tOSTemplate.cLabel=%s exists. Not adding.\n",fieldExt[3]); |
|---|
| 2647 | if(!uOSTemplate) |
|---|
| 2648 | { |
|---|
| 2649 | printf("Unexpected uOSTemplate==0 error skip to next container\n"); |
|---|
| 2650 | continue; |
|---|
| 2651 | } |
|---|
| 2652 | } |
|---|
| 2653 | mysql_free_result(res); |
|---|
| 2654 | |
|---|
| 2655 | //debug only |
|---|
| 2656 | //printf("Post tOSTemplate import stop uOSTemplate=%u\n",uOSTemplate); |
|---|
| 2657 | //exit(0); |
|---|
| 2658 | |
|---|
| 2659 | //tConfig.cLabel [4] |
|---|
| 2660 | sprintf(gcQuery,"SELECT uConfig FROM tConfig WHERE cLabel='%s'",fieldExt[4]); |
|---|
| 2661 | mysql_query(&gMysql,gcQuery); |
|---|
| 2662 | if(mysql_errno(&gMysql)) |
|---|
| 2663 | { |
|---|
| 2664 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2665 | exit(1); |
|---|
| 2666 | } |
|---|
| 2667 | res=mysql_store_result(&gMysql); |
|---|
| 2668 | if(mysql_num_rows(res)==0) |
|---|
| 2669 | { |
|---|
| 2670 | sprintf(gcQuery,"INSERT INTO tConfig SET cLabel='%s',uOwner=%u," |
|---|
| 2671 | "uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2672 | fieldExt[4],uClient); |
|---|
| 2673 | mysql_query(&gMysql,gcQuery); |
|---|
| 2674 | if(mysql_errno(&gMysql)) |
|---|
| 2675 | { |
|---|
| 2676 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2677 | exit(1); |
|---|
| 2678 | } |
|---|
| 2679 | uConfig=mysql_insert_id(&gMysql); |
|---|
| 2680 | //Enable for only this datacenter via tProperty tConfig type = 6 TODO defines |
|---|
| 2681 | sprintf(gcQuery,"INSERT INTO tProperty SET cName='cDatacenter',cValue='%s',uType=6,uKey=%u," |
|---|
| 2682 | "uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2683 | cLocalDatacenter,uConfig,uClient); |
|---|
| 2684 | mysql_query(&gMysql,gcQuery); |
|---|
| 2685 | if(mysql_errno(&gMysql)) |
|---|
| 2686 | { |
|---|
| 2687 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2688 | exit(1); |
|---|
| 2689 | } |
|---|
| 2690 | } |
|---|
| 2691 | else |
|---|
| 2692 | { |
|---|
| 2693 | if((field=mysql_fetch_row(res))) |
|---|
| 2694 | sscanf(field[0],"%u",&uConfig); |
|---|
| 2695 | //printf("Local tConfig.cLabel=%s exists. Not adding.\n",fieldExt[4]); |
|---|
| 2696 | if(!uConfig) |
|---|
| 2697 | { |
|---|
| 2698 | printf("Unexpected uConfig==0 error skip to next container\n"); |
|---|
| 2699 | continue; |
|---|
| 2700 | } |
|---|
| 2701 | } |
|---|
| 2702 | mysql_free_result(res); |
|---|
| 2703 | |
|---|
| 2704 | //debug only |
|---|
| 2705 | //printf("Post tConfig import stop uConfig=%u\n",uConfig); |
|---|
| 2706 | //exit(0); |
|---|
| 2707 | |
|---|
| 2708 | //tNameserver.cLabel [5] |
|---|
| 2709 | sprintf(gcQuery,"SELECT uNameserver FROM tNameserver WHERE cLabel='%s'",fieldExt[5]); |
|---|
| 2710 | mysql_query(&gMysql,gcQuery); |
|---|
| 2711 | if(mysql_errno(&gMysql)) |
|---|
| 2712 | { |
|---|
| 2713 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2714 | exit(1); |
|---|
| 2715 | } |
|---|
| 2716 | res=mysql_store_result(&gMysql); |
|---|
| 2717 | if(mysql_num_rows(res)==0) |
|---|
| 2718 | { |
|---|
| 2719 | sprintf(gcQuery,"INSERT INTO tNameserver SET cLabel='%s',uOwner=%u," |
|---|
| 2720 | "uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2721 | fieldExt[5],uClient); |
|---|
| 2722 | mysql_query(&gMysql,gcQuery); |
|---|
| 2723 | if(mysql_errno(&gMysql)) |
|---|
| 2724 | { |
|---|
| 2725 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2726 | exit(1); |
|---|
| 2727 | } |
|---|
| 2728 | uNameserver=mysql_insert_id(&gMysql); |
|---|
| 2729 | //Enable for only this datacenter via tProperty tNameserver type = 7 TODO defines |
|---|
| 2730 | sprintf(gcQuery,"INSERT INTO tProperty SET cName='cDatacenter',cValue='%s',uType=7,uKey=%u," |
|---|
| 2731 | " uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2732 | cLocalDatacenter,uNameserver,uClient); |
|---|
| 2733 | mysql_query(&gMysql,gcQuery); |
|---|
| 2734 | if(mysql_errno(&gMysql)) |
|---|
| 2735 | { |
|---|
| 2736 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2737 | exit(1); |
|---|
| 2738 | } |
|---|
| 2739 | } |
|---|
| 2740 | else |
|---|
| 2741 | { |
|---|
| 2742 | if((field=mysql_fetch_row(res))) |
|---|
| 2743 | sscanf(field[0],"%u",&uNameserver); |
|---|
| 2744 | //printf("Local tNameserver.cLabel=%s exists. Not adding.\n",fieldExt[5]); |
|---|
| 2745 | if(!uNameserver) |
|---|
| 2746 | { |
|---|
| 2747 | printf("Unexpected uNameserver==0 error skip to next container\n"); |
|---|
| 2748 | continue; |
|---|
| 2749 | } |
|---|
| 2750 | } |
|---|
| 2751 | |
|---|
| 2752 | //debug only |
|---|
| 2753 | //printf("Post tNameserver import stop uNameserver=%u\n",uNameserver); |
|---|
| 2754 | //exit(0); |
|---|
| 2755 | |
|---|
| 2756 | //tSearchdomain.cLabel [6] |
|---|
| 2757 | sprintf(gcQuery,"SELECT uSearchdomain FROM tSearchdomain WHERE cLabel='%s'",fieldExt[6]); |
|---|
| 2758 | mysql_query(&gMysql,gcQuery); |
|---|
| 2759 | if(mysql_errno(&gMysql)) |
|---|
| 2760 | { |
|---|
| 2761 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2762 | exit(1); |
|---|
| 2763 | } |
|---|
| 2764 | res=mysql_store_result(&gMysql); |
|---|
| 2765 | if(mysql_num_rows(res)==0) |
|---|
| 2766 | { |
|---|
| 2767 | sprintf(gcQuery,"INSERT INTO tSearchdomain SET cLabel='%s',uOwner=%u," |
|---|
| 2768 | "uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2769 | fieldExt[6],uClient); |
|---|
| 2770 | mysql_query(&gMysql,gcQuery); |
|---|
| 2771 | if(mysql_errno(&gMysql)) |
|---|
| 2772 | { |
|---|
| 2773 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2774 | exit(1); |
|---|
| 2775 | } |
|---|
| 2776 | uSearchdomain=mysql_insert_id(&gMysql); |
|---|
| 2777 | //Enable for only this datacenter via tProperty tSearchdomain type = 9 TODO defines |
|---|
| 2778 | sprintf(gcQuery,"INSERT INTO tProperty SET cName='cDatacenter',cValue='%s',uType=9,uKey=%u," |
|---|
| 2779 | "uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2780 | cLocalDatacenter,uSearchdomain,uClient); |
|---|
| 2781 | mysql_query(&gMysql,gcQuery); |
|---|
| 2782 | if(mysql_errno(&gMysql)) |
|---|
| 2783 | { |
|---|
| 2784 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2785 | exit(1); |
|---|
| 2786 | } |
|---|
| 2787 | } |
|---|
| 2788 | else |
|---|
| 2789 | { |
|---|
| 2790 | if((field=mysql_fetch_row(res))) |
|---|
| 2791 | sscanf(field[0],"%u",&uSearchdomain); |
|---|
| 2792 | //printf("Local tSearchdomain.cLabel=%s exists. Not adding.\n",fieldExt[6]); |
|---|
| 2793 | if(!uSearchdomain) |
|---|
| 2794 | { |
|---|
| 2795 | printf("Unexpected uSearchdomain==0 error skip to next container\n"); |
|---|
| 2796 | continue; |
|---|
| 2797 | } |
|---|
| 2798 | } |
|---|
| 2799 | |
|---|
| 2800 | //debug only |
|---|
| 2801 | //printf("Post tSearchdomain import stop uSearchdomain=%u\n",uSearchdomain); |
|---|
| 2802 | //exit(0); |
|---|
| 2803 | |
|---|
| 2804 | //Add container |
|---|
| 2805 | sprintf(gcQuery,"INSERT INTO tContainer SET" |
|---|
| 2806 | " cLabel='%s'," |
|---|
| 2807 | " cHostname='%s'," |
|---|
| 2808 | " uVeth=0," |
|---|
| 2809 | " uIPv4=%u," |
|---|
| 2810 | " uOSTemplate=%u," |
|---|
| 2811 | " uConfig=%u," |
|---|
| 2812 | " uNameserver=%u," |
|---|
| 2813 | " uSearchdomain=%u," |
|---|
| 2814 | " uDatacenter=%u," |
|---|
| 2815 | " uNode=%u," |
|---|
| 2816 | " uStatus=3," //Offline |
|---|
| 2817 | " uSource=0," |
|---|
| 2818 | " uOwner=%u," |
|---|
| 2819 | " uCreatedBy=1," |
|---|
| 2820 | " uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 2821 | fieldExt[0], |
|---|
| 2822 | fieldExt[1], |
|---|
| 2823 | uIP, |
|---|
| 2824 | uOSTemplate, |
|---|
| 2825 | uConfig, |
|---|
| 2826 | uNameserver, |
|---|
| 2827 | uSearchdomain, |
|---|
| 2828 | uLocalDatacenter, |
|---|
| 2829 | uLocalNode, |
|---|
| 2830 | uClient); |
|---|
| 2831 | mysql_query(&gMysql,gcQuery); |
|---|
| 2832 | if(mysql_errno(&gMysql)) |
|---|
| 2833 | { |
|---|
| 2834 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2835 | exit(1); |
|---|
| 2836 | } |
|---|
| 2837 | printf("Imported: %s\n",fieldExt[1]); |
|---|
| 2838 | uContainer=mysql_insert_id(&gMysql); |
|---|
| 2839 | if(!uContainer) |
|---|
| 2840 | { |
|---|
| 2841 | printf("Unexpected uContainer==0 error skipping group section\n"); |
|---|
| 2842 | continue; |
|---|
| 2843 | } |
|---|
| 2844 | |
|---|
| 2845 | //Add container to premade optional group "cRemoteDatacenter" |
|---|
| 2846 | if(uGroup) |
|---|
| 2847 | { |
|---|
| 2848 | sprintf(gcQuery,"INSERT INTO tGroupGlue SET uGroup=%u,uContainer=%u",uGroup,uContainer); |
|---|
| 2849 | mysql_query(&gMysql,gcQuery); |
|---|
| 2850 | if(mysql_errno(&gMysql)) |
|---|
| 2851 | { |
|---|
| 2852 | printf("%s\n",mysql_error(&gMysql)); |
|---|
| 2853 | exit(1); |
|---|
| 2854 | } |
|---|
| 2855 | } |
|---|
| 2856 | |
|---|
| 2857 | }//While remote containers |
|---|
| 2858 | mysql_free_result(resExt); |
|---|
| 2859 | |
|---|
| 2860 | |
|---|
| 2861 | mysql_close(&gMysql); |
|---|
| 2862 | mysql_close(&gMysqlExt); |
|---|
| 2863 | printf("ImportRemoteDatacenter(): End\n"); |
|---|
| 2864 | |
|---|
| 2865 | }//void ImportRemoteDatacenter() |
|---|
| 2866 | |
|---|
| 2867 | |
|---|
| 2868 | void ImportOSTemplates(char *cPath, char *cOwner) |
|---|
| 2869 | { |
|---|
| 2870 | char cHostname[100]={""}; |
|---|
| 2871 | char cLabel[100]={""}; |
|---|
| 2872 | unsigned uOwner=0; |
|---|
| 2873 | unsigned uNode=0; |
|---|
| 2874 | unsigned uDatacenter=0; |
|---|
| 2875 | struct dirent **namelist; |
|---|
| 2876 | register int n,i; |
|---|
| 2877 | char *cp; |
|---|
| 2878 | MYSQL_RES *res; |
|---|
| 2879 | MYSQL_ROW field; |
|---|
| 2880 | |
|---|
| 2881 | printf("ImportOSTemplates(): Start\n"); |
|---|
| 2882 | |
|---|
| 2883 | if(TextConnectDb()) |
|---|
| 2884 | exit(1); |
|---|
| 2885 | |
|---|
| 2886 | if(gethostname(cHostname,99)!=0) |
|---|
| 2887 | { |
|---|
| 2888 | printf("gethostname() failed\n"); |
|---|
| 2889 | exit(1); |
|---|
| 2890 | } |
|---|
| 2891 | |
|---|
| 2892 | //Get node and datacenter via hostname |
|---|
| 2893 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 2894 | mysql_query(&gMysql,gcQuery); |
|---|
| 2895 | if(mysql_errno(&gMysql)) |
|---|
| 2896 | { |
|---|
| 2897 | printf(mysql_error(&gMysql)); |
|---|
| 2898 | mysql_close(&gMysql); |
|---|
| 2899 | exit(2); |
|---|
| 2900 | } |
|---|
| 2901 | res=mysql_store_result(&gMysql); |
|---|
| 2902 | if((field=mysql_fetch_row(res))) |
|---|
| 2903 | { |
|---|
| 2904 | sscanf(field[0],"%u",&uNode); |
|---|
| 2905 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 2906 | } |
|---|
| 2907 | mysql_free_result(res); |
|---|
| 2908 | //FQDN vs short name of 2nd NIC mess |
|---|
| 2909 | if(!uNode) |
|---|
| 2910 | { |
|---|
| 2911 | if((cp=strchr(cHostname,'.'))) |
|---|
| 2912 | *cp=0; |
|---|
| 2913 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 2914 | mysql_query(&gMysql,gcQuery); |
|---|
| 2915 | if(mysql_errno(&gMysql)) |
|---|
| 2916 | { |
|---|
| 2917 | printf(mysql_error(&gMysql)); |
|---|
| 2918 | mysql_close(&gMysql); |
|---|
| 2919 | exit(2); |
|---|
| 2920 | } |
|---|
| 2921 | res=mysql_store_result(&gMysql); |
|---|
| 2922 | if((field=mysql_fetch_row(res))) |
|---|
| 2923 | { |
|---|
| 2924 | sscanf(field[0],"%u",&uNode); |
|---|
| 2925 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 2926 | } |
|---|
| 2927 | mysql_free_result(res); |
|---|
| 2928 | } |
|---|
| 2929 | |
|---|
| 2930 | //Get uOwner |
|---|
| 2931 | sprintf(gcQuery,"SELECT uClient FROM tClient WHERE cLabel='%s'",cOwner); |
|---|
| 2932 | mysql_query(&gMysql,gcQuery); |
|---|
| 2933 | if(mysql_errno(&gMysql)) |
|---|
| 2934 | { |
|---|
| 2935 | printf(mysql_error(&gMysql)); |
|---|
| 2936 | mysql_close(&gMysql); |
|---|
| 2937 | exit(2); |
|---|
| 2938 | } |
|---|
| 2939 | res=mysql_store_result(&gMysql); |
|---|
| 2940 | if((field=mysql_fetch_row(res))) |
|---|
| 2941 | sscanf(field[0],"%u",&uOwner); |
|---|
| 2942 | mysql_free_result(res); |
|---|
| 2943 | if(!uOwner) |
|---|
| 2944 | { |
|---|
| 2945 | printf("No such owner: \"%s\"\n",cOwner); |
|---|
| 2946 | exit(1); |
|---|
| 2947 | } |
|---|
| 2948 | printf("uNode=%u uDatacenter=%u uOwner=%u\n",uNode,uDatacenter,uOwner); |
|---|
| 2949 | |
|---|
| 2950 | n=scandir(cPath,&namelist,0,0); |
|---|
| 2951 | if(n<0) |
|---|
| 2952 | { |
|---|
| 2953 | printf("scandir() error.\n"); |
|---|
| 2954 | exit(1); |
|---|
| 2955 | } |
|---|
| 2956 | else if(n==2) |
|---|
| 2957 | { |
|---|
| 2958 | printf("No files found.\n"); |
|---|
| 2959 | exit(1); |
|---|
| 2960 | } |
|---|
| 2961 | for(i=0;i<n;i++) |
|---|
| 2962 | { |
|---|
| 2963 | |
|---|
| 2964 | //Added some end of list test hack a long time ago. Remove? |
|---|
| 2965 | if(namelist[i]->d_name[0]=='.' || |
|---|
| 2966 | strstr(namelist[i]->d_name+strlen(namelist[i]->d_name)-5, |
|---|
| 2967 | ".done")) |
|---|
| 2968 | { |
|---|
| 2969 | ; |
|---|
| 2970 | } |
|---|
| 2971 | else |
|---|
| 2972 | { |
|---|
| 2973 | if(!strcmp(namelist[i]->d_name+strlen(namelist[i]->d_name)-6,"tar.gz")) |
|---|
| 2974 | { |
|---|
| 2975 | unsigned uOSTemplate=0; |
|---|
| 2976 | |
|---|
| 2977 | sprintf(cLabel,"%.100s",namelist[i]->d_name); |
|---|
| 2978 | |
|---|
| 2979 | //Chop off .tar.gz |
|---|
| 2980 | if((cp=strstr(cLabel,".tar.gz"))) |
|---|
| 2981 | *cp=0; |
|---|
| 2982 | |
|---|
| 2983 | //Ignore if already in tOSTemplate |
|---|
| 2984 | sprintf(gcQuery,"SELECT uOSTemplate FROM tOSTemplate WHERE cLabel='%s'",cLabel); |
|---|
| 2985 | mysql_query(&gMysql,gcQuery); |
|---|
| 2986 | if(mysql_errno(&gMysql)) |
|---|
| 2987 | { |
|---|
| 2988 | printf(mysql_error(&gMysql)); |
|---|
| 2989 | mysql_close(&gMysql); |
|---|
| 2990 | exit(2); |
|---|
| 2991 | } |
|---|
| 2992 | res=mysql_store_result(&gMysql); |
|---|
| 2993 | if(mysql_num_rows(res)) |
|---|
| 2994 | { |
|---|
| 2995 | mysql_free_result(res); |
|---|
| 2996 | continue; |
|---|
| 2997 | } |
|---|
| 2998 | mysql_free_result(res); |
|---|
| 2999 | |
|---|
| 3000 | //Add new record |
|---|
| 3001 | sprintf(gcQuery,"INSERT INTO tOSTemplate SET cLabel='%s',uOwner=%u,uCreatedBy=1," |
|---|
| 3002 | "uCreatedDate=UNIX_TIMESTAMP(NOW())",TextAreaSave(cLabel),uOwner); |
|---|
| 3003 | mysql_query(&gMysql,gcQuery); |
|---|
| 3004 | if(mysql_errno(&gMysql)) |
|---|
| 3005 | { |
|---|
| 3006 | printf(mysql_error(&gMysql)); |
|---|
| 3007 | mysql_close(&gMysql); |
|---|
| 3008 | exit(2); |
|---|
| 3009 | } |
|---|
| 3010 | uOSTemplate=mysql_insert_id(&gMysql); |
|---|
| 3011 | sprintf(gcQuery,"INSERT INTO tProperty SET uKey=%u,uType="PROP_OSTEMPLATE |
|---|
| 3012 | ",cName='cDatacenter',cValue='%s',uOwner=%u,uCreatedBy=1" |
|---|
| 3013 | ",uCreatedDate=UNIX_TIMESTAMP(NOW())" |
|---|
| 3014 | ,uOSTemplate,ForeignKey("tDatacenter","cLabel",uDatacenter),uOwner); |
|---|
| 3015 | mysql_query(&gMysql,gcQuery); |
|---|
| 3016 | if(mysql_errno(&gMysql)) |
|---|
| 3017 | { |
|---|
| 3018 | printf(mysql_error(&gMysql)); |
|---|
| 3019 | mysql_close(&gMysql); |
|---|
| 3020 | exit(2); |
|---|
| 3021 | } |
|---|
| 3022 | printf("%s\n",cLabel); |
|---|
| 3023 | } |
|---|
| 3024 | } |
|---|
| 3025 | } |
|---|
| 3026 | |
|---|
| 3027 | printf("ImportOSTemplates(): End\n"); |
|---|
| 3028 | |
|---|
| 3029 | }//void ImportOSTemplates() |
|---|
| 3030 | |
|---|
| 3031 | |
|---|
| 3032 | void MassCreateContainers(char *cConfigfileName) |
|---|
| 3033 | { |
|---|
| 3034 | char cHostname[100]={""}; |
|---|
| 3035 | unsigned uNode=0; |
|---|
| 3036 | unsigned uDatacenter=0; |
|---|
| 3037 | char *cp; |
|---|
| 3038 | FILE *fp; |
|---|
| 3039 | MYSQL_RES *res; |
|---|
| 3040 | MYSQL_ROW field; |
|---|
| 3041 | |
|---|
| 3042 | printf("MassCreateContainers(): Start\n\n"); |
|---|
| 3043 | |
|---|
| 3044 | if(TextConnectDb()) |
|---|
| 3045 | exit(1); |
|---|
| 3046 | |
|---|
| 3047 | if(gethostname(cHostname,99)!=0) |
|---|
| 3048 | { |
|---|
| 3049 | printf("gethostname() failed\n"); |
|---|
| 3050 | exit(1); |
|---|
| 3051 | } |
|---|
| 3052 | |
|---|
| 3053 | // |
|---|
| 3054 | //Get node and datacenter via hostname |
|---|
| 3055 | // |
|---|
| 3056 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 3057 | mysql_query(&gMysql,gcQuery); |
|---|
| 3058 | if(mysql_errno(&gMysql)) |
|---|
| 3059 | { |
|---|
| 3060 | printf(mysql_error(&gMysql)); |
|---|
| 3061 | mysql_close(&gMysql); |
|---|
| 3062 | exit(2); |
|---|
| 3063 | } |
|---|
| 3064 | res=mysql_store_result(&gMysql); |
|---|
| 3065 | if((field=mysql_fetch_row(res))) |
|---|
| 3066 | { |
|---|
| 3067 | sscanf(field[0],"%u",&uNode); |
|---|
| 3068 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 3069 | } |
|---|
| 3070 | mysql_free_result(res); |
|---|
| 3071 | //FQDN vs short name of 2nd NIC mess |
|---|
| 3072 | if(!uNode) |
|---|
| 3073 | { |
|---|
| 3074 | if((cp=strchr(cHostname,'.'))) |
|---|
| 3075 | *cp=0; |
|---|
| 3076 | sprintf(gcQuery,"SELECT uNode,uDatacenter FROM tNode WHERE cLabel='%.99s'",cHostname); |
|---|
| 3077 | mysql_query(&gMysql,gcQuery); |
|---|
| 3078 | if(mysql_errno(&gMysql)) |
|---|
| 3079 | { |
|---|
| 3080 | printf(mysql_error(&gMysql)); |
|---|
| 3081 | mysql_close(&gMysql); |
|---|
| 3082 | exit(2); |
|---|
| 3083 | } |
|---|
| 3084 | res=mysql_store_result(&gMysql); |
|---|
| 3085 | if((field=mysql_fetch_row(res))) |
|---|
| 3086 | { |
|---|
| 3087 | sscanf(field[0],"%u",&uNode); |
|---|
| 3088 | sscanf(field[1],"%u",&uDatacenter); |
|---|
| 3089 | } |
|---|
| 3090 | mysql_free_result(res); |
|---|
| 3091 | } |
|---|
| 3092 | printf("uNode=%u uDatacenter=%u\n",uNode,uDatacenter); |
|---|
| 3093 | |
|---|
| 3094 | // |
|---|
| 3095 | //Now we can read the config file |
|---|
| 3096 | // |
|---|
| 3097 | if((fp=fopen(cConfigfileName,"r"))==NULL) |
|---|
| 3098 | { |
|---|
| 3099 | printf("Could not open %s\n",cConfigfileName); |
|---|
| 3100 | exit(1); |
|---|
| 3101 | } |
|---|
| 3102 | |
|---|
| 3103 | |
|---|
| 3104 | //config file has two sections |
|---|
| 3105 | unsigned uList=0; |
|---|
| 3106 | |
|---|
| 3107 | //these two are for section two |
|---|
| 3108 | char cLabel[33]={""}; |
|---|
| 3109 | char cCloneLabel[33]={""}; |
|---|
| 3110 | char cCloneHostname[100]={""}; |
|---|
| 3111 | char cAltLabel[33]={""}; |
|---|
| 3112 | //char cHostname[100]={""}; |
|---|
| 3113 | |
|---|
| 3114 | char cIPv4ClassC[16]={""}; |
|---|
| 3115 | unsigned uIPv4=0; |
|---|
| 3116 | |
|---|
| 3117 | char cPasswd[16]={""}; |
|---|
| 3118 | char cTimeZone[32]={""};//PST8PDT |
|---|
| 3119 | |
|---|
| 3120 | char cIPv4CloneClassC[16]={""}; |
|---|
| 3121 | unsigned uCloneIPv4=0; |
|---|
| 3122 | |
|---|
| 3123 | unsigned uConfig=0; |
|---|
| 3124 | unsigned uNameserver=0; |
|---|
| 3125 | unsigned uSearchdomain=0; |
|---|
| 3126 | unsigned uGroup=0; |
|---|
| 3127 | unsigned uOwner=0; |
|---|
| 3128 | unsigned uOSTemplate=0; |
|---|
| 3129 | unsigned uOSLoopTemplate=0; |
|---|
| 3130 | unsigned uDNSJob=0; |
|---|
| 3131 | unsigned uSyncPeriod=0; |
|---|
| 3132 | unsigned uCloneTargetNode=0; |
|---|
| 3133 | unsigned uCloneStopped=1; |
|---|
| 3134 | |
|---|
| 3135 | while(fgets(gcQuery,1024,fp)!=NULL) |
|---|
| 3136 | { |
|---|
| 3137 | if(gcQuery[0]=='#') |
|---|
| 3138 | continue; |
|---|
| 3139 | |
|---|
| 3140 | if(!strncmp(gcQuery,"ContainerList",strlen("ContainerList"))) |
|---|
| 3141 | { |
|---|
| 3142 | uList=1; |
|---|
| 3143 | printf("Global settings\n"); |
|---|
| 3144 | printf("\tcIPv4ClassC=%s\n",cIPv4ClassC); |
|---|
| 3145 | printf("\tcIPv4CloneClassC=%s\n",cIPv4CloneClassC); |
|---|
| 3146 | printf("\tcPasswd=%s\n",cPasswd); |
|---|
| 3147 | printf("\tcTimeZone=%s\n",cTimeZone); |
|---|
| 3148 | printf("\tuOwner=%u\n",uOwner); |
|---|
| 3149 | printf("\tuConfig=%u\n",uConfig); |
|---|
| 3150 | printf("\tuNameserver=%u\n",uNameserver); |
|---|
| 3151 | printf("\tuSearchdomain=%u\n",uSearchdomain); |
|---|
| 3152 | printf("\tuGroup=%u\n",uGroup); |
|---|
| 3153 | printf("\tuOSTemplate=%u\n",uOSTemplate); |
|---|
| 3154 | printf("\tuSyncPeriod=%u\n",uSyncPeriod); |
|---|
| 3155 | printf("\tuDNSJob=%u\n",uDNSJob); |
|---|
| 3156 | printf("\tuCloneTargetNode=%u\n",uCloneTargetNode); |
|---|
| 3157 | printf("\tuCloneStopped=%u\n",uCloneStopped); |
|---|
| 3158 | continue; |
|---|
| 3159 | } |
|---|
| 3160 | |
|---|
| 3161 | if(!uList) |
|---|
| 3162 | { |
|---|
| 3163 | if(!strncmp(gcQuery,"cIPv4ClassC=",strlen("cIPv4ClassC"))) |
|---|
| 3164 | { |
|---|
| 3165 | sprintf(cIPv4ClassC,"%.15s",gcQuery+strlen("cIPv4ClassC=")); |
|---|
| 3166 | if((cp=strchr(cIPv4ClassC,';'))) |
|---|
| 3167 | *cp=0; |
|---|
| 3168 | } |
|---|
| 3169 | else if(!strncmp(gcQuery,"cIPv4CloneClassC=",strlen("cIPv4CloneClassC"))) |
|---|
| 3170 | { |
|---|
| 3171 | sprintf(cIPv4CloneClassC,"%.15s",gcQuery+strlen("cIPv4CloneClassC=")); |
|---|
| 3172 | if((cp=strchr(cIPv4CloneClassC,';'))) |
|---|
| 3173 | *cp=0; |
|---|
| 3174 | } |
|---|
| 3175 | else if(!strncmp(gcQuery,"cPasswd=",strlen("cPasswd"))) |
|---|
| 3176 | { |
|---|
| 3177 | sprintf(cPasswd,"%.15s",gcQuery+strlen("cPasswd=")); |
|---|
| 3178 | if((cp=strchr(cPasswd,';'))) |
|---|
| 3179 | *cp=0; |
|---|
| 3180 | } |
|---|
| 3181 | else if(!strncmp(gcQuery,"cTimeZone=",strlen("cTimeZone"))) |
|---|
| 3182 | { |
|---|
| 3183 | sprintf(cTimeZone,"%.31s",gcQuery+strlen("cTimeZone=")); |
|---|
| 3184 | if((cp=strchr(cTimeZone,';'))) |
|---|
| 3185 | *cp=0; |
|---|
| 3186 | } |
|---|
| 3187 | else if(!strncmp(gcQuery,"uOwner=",strlen("uOwner="))) |
|---|
| 3188 | sscanf(gcQuery,"uOwner=%u;",&uOwner); |
|---|
| 3189 | else if(!strncmp(gcQuery,"uConfig=",strlen("uConfig="))) |
|---|
| 3190 | sscanf(gcQuery,"uConfig=%u;",&uConfig); |
|---|
| 3191 | else if(!strncmp(gcQuery,"uNameserver=",strlen("uNameserver="))) |
|---|
| 3192 | sscanf(gcQuery,"uNameserver=%u;",&uNameserver); |
|---|
| 3193 | else if(!strncmp(gcQuery,"uSearchdomain=",strlen("uSearchdomain="))) |
|---|
| 3194 | sscanf(gcQuery,"uSearchdomain=%u;",&uSearchdomain); |
|---|
| 3195 | else if(!strncmp(gcQuery,"uGroup=",strlen("uGroup="))) |
|---|
| 3196 | sscanf(gcQuery,"uGroup=%u;",&uGroup); |
|---|
| 3197 | else if(!strncmp(gcQuery,"uOSTemplate=",strlen("uOSTemplate="))) |
|---|
| 3198 | sscanf(gcQuery,"uOSTemplate=%u;",&uOSTemplate); |
|---|
| 3199 | else if(!strncmp(gcQuery,"uSyncPeriod=",strlen("uSyncPeriod="))) |
|---|
| 3200 | sscanf(gcQuery,"uSyncPeriod=%u;",&uSyncPeriod); |
|---|
| 3201 | else if(!strncmp(gcQuery,"uDNSJob=",strlen("uDNSJob="))) |
|---|
| 3202 | sscanf(gcQuery,"uDNSJob=%u;",&uDNSJob); |
|---|
| 3203 | else if(!strncmp(gcQuery,"uCloneTargetNode=",strlen("uCloneTargetNode="))) |
|---|
| 3204 | sscanf(gcQuery,"uCloneTargetNode=%u;",&uCloneTargetNode); |
|---|
| 3205 | else if(!strncmp(gcQuery,"uCloneStopped=",strlen("uCloneStopped="))) |
|---|
| 3206 | sscanf(gcQuery,"uCloneStopped=%u;",&uCloneStopped); |
|---|
| 3207 | } |
|---|
| 3208 | else |
|---|
| 3209 | { |
|---|
| 3210 | cHostname[0]=0; |
|---|
| 3211 | cLabel[0]=0; |
|---|
| 3212 | cAltLabel[0]=0; |
|---|
| 3213 | if((cp=strchr(gcQuery,'.'))) |
|---|
| 3214 | { |
|---|
| 3215 | *cp=0; |
|---|
| 3216 | sprintf(cLabel,"%.31s",gcQuery); |
|---|
| 3217 | *cp='.'; |
|---|
| 3218 | if((cp=strchr(gcQuery,','))) |
|---|
| 3219 | { |
|---|
| 3220 | *cp=0; |
|---|
| 3221 | sprintf(cAltLabel,"%.31s",cp+1); |
|---|
| 3222 | } |
|---|
| 3223 | sprintf(cHostname,"%.99s",gcQuery); |
|---|
| 3224 | if((cp=strchr(cHostname,'\n'))) |
|---|
| 3225 | *cp=0; |
|---|
| 3226 | if((cp=strchr(cAltLabel,'\n'))) |
|---|
| 3227 | *cp=0; |
|---|
| 3228 | printf("\ncHostname=%s cLabel=%s",cHostname,cLabel); |
|---|
| 3229 | if(cAltLabel[0]) |
|---|
| 3230 | printf(" cAltLabel=%s\n",cAltLabel); |
|---|
| 3231 | else |
|---|
| 3232 | printf("\n"); |
|---|
| 3233 | |
|---|
| 3234 | |
|---|
| 3235 | // |
|---|
| 3236 | //Gather data from config settings |
|---|
| 3237 | // |
|---|
| 3238 | |
|---|
| 3239 | //Get some available IPs for this new container |
|---|
| 3240 | if(!cIPv4ClassC[0]) |
|---|
| 3241 | { |
|---|
| 3242 | printf("cIPv4ClassC must be specified\n"); |
|---|
| 3243 | continue; |
|---|
| 3244 | } |
|---|
| 3245 | if(!cIPv4CloneClassC[0]) |
|---|
| 3246 | { |
|---|
| 3247 | printf("cIPv4CloneClassC must be specified\n"); |
|---|
| 3248 | continue; |
|---|
| 3249 | } |
|---|
| 3250 | sprintf(gcQuery,"SELECT uIP,cLabel FROM tIP WHERE uAvailable=1 AND" |
|---|
| 3251 | " cLabel LIKE '%s%%'" |
|---|
| 3252 | " AND uOwner=%u AND uDatacenter=%u",cIPv4ClassC,uOwner,uDatacenter); |
|---|
| 3253 | mysql_query(&gMysql,gcQuery); |
|---|
| 3254 | if(mysql_errno(&gMysql)) |
|---|
| 3255 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3256 | res=mysql_store_result(&gMysql); |
|---|
| 3257 | if((field=mysql_fetch_row(res))) |
|---|
| 3258 | { |
|---|
| 3259 | sscanf(field[0],"%u",&uIPv4); |
|---|
| 3260 | printf("cIPv4ClassC IP %s\n",field[1]); |
|---|
| 3261 | } |
|---|
| 3262 | mysql_free_result(res); |
|---|
| 3263 | sprintf(gcQuery,"SELECT uIP,cLabel FROM tIP WHERE uAvailable=1 AND" |
|---|
| 3264 | " cLabel LIKE '%s%%'" |
|---|
| 3265 | " AND uOwner=%u AND uDatacenter=%u",cIPv4CloneClassC,uOwner,uDatacenter); |
|---|
| 3266 | mysql_query(&gMysql,gcQuery); |
|---|
| 3267 | if(mysql_errno(&gMysql)) |
|---|
| 3268 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3269 | res=mysql_store_result(&gMysql); |
|---|
| 3270 | if((field=mysql_fetch_row(res))) |
|---|
| 3271 | { |
|---|
| 3272 | sscanf(field[0],"%u",&uCloneIPv4); |
|---|
| 3273 | printf("cIPv4CloneClassC IP %s\n",field[1]); |
|---|
| 3274 | } |
|---|
| 3275 | mysql_free_result(res); |
|---|
| 3276 | |
|---|
| 3277 | if(!uOSTemplate) |
|---|
| 3278 | { |
|---|
| 3279 | uOSLoopTemplate=0; |
|---|
| 3280 | |
|---|
| 3281 | if(!cAltLabel[0]) |
|---|
| 3282 | sprintf(cAltLabel,"%31s",cLabel); |
|---|
| 3283 | |
|---|
| 3284 | //Try to find a matching tOSTemplate |
|---|
| 3285 | sprintf(gcQuery,"SELECT uOSTemplate,cLabel FROM tOSTemplate WHERE " |
|---|
| 3286 | " cLabel LIKE '%%%s%%'",cAltLabel); |
|---|
| 3287 | mysql_query(&gMysql,gcQuery); |
|---|
| 3288 | if(mysql_errno(&gMysql)) |
|---|
| 3289 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3290 | res=mysql_store_result(&gMysql); |
|---|
| 3291 | if(mysql_num_rows(res)==1) |
|---|
| 3292 | { |
|---|
| 3293 | field=mysql_fetch_row(res); |
|---|
| 3294 | sscanf(field[0],"%u",&uOSLoopTemplate); |
|---|
| 3295 | printf("tOSTemplate %s\n",field[1]); |
|---|
| 3296 | } |
|---|
| 3297 | else |
|---|
| 3298 | { |
|---|
| 3299 | printf("No single tOSTemplate match found based on:%s\n",cAltLabel); |
|---|
| 3300 | //printf("%s\n",gcQuery); |
|---|
| 3301 | } |
|---|
| 3302 | mysql_free_result(res); |
|---|
| 3303 | } |
|---|
| 3304 | else |
|---|
| 3305 | { |
|---|
| 3306 | uOSLoopTemplate=uOSTemplate; |
|---|
| 3307 | } |
|---|
| 3308 | |
|---|
| 3309 | // |
|---|
| 3310 | //here we start creating the new container jobs |
|---|
| 3311 | //after input validation |
|---|
| 3312 | // |
|---|
| 3313 | if(uDatacenter==0) |
|---|
| 3314 | { |
|---|
| 3315 | printf("Unexpected uDatacenter==0!\n"); |
|---|
| 3316 | continue; |
|---|
| 3317 | } |
|---|
| 3318 | if(uNode==0) |
|---|
| 3319 | { |
|---|
| 3320 | printf("Unexpected uNode==0!\n"); |
|---|
| 3321 | continue; |
|---|
| 3322 | } |
|---|
| 3323 | |
|---|
| 3324 | char cNCMDatacenter[256]={""}; |
|---|
| 3325 | GetDatacenterProp(uDatacenter,"NewContainerMode",cNCMDatacenter); |
|---|
| 3326 | if(cNCMDatacenter[0] && strcmp(cNCMDatacenter,"Active")) |
|---|
| 3327 | { |
|---|
| 3328 | printf("Selected datacenter is full or not active. Select another.\n"); |
|---|
| 3329 | continue; |
|---|
| 3330 | } |
|---|
| 3331 | |
|---|
| 3332 | char cNCMNode[256]={""}; |
|---|
| 3333 | GetNodeProp(uNode,"NewContainerMode",cNCMNode); |
|---|
| 3334 | if(cNCMNode[0] && strcmp(cNCMNode,"Active")) |
|---|
| 3335 | { |
|---|
| 3336 | printf("Selected node is not configured for active containers." |
|---|
| 3337 | "Select another.\n"); |
|---|
| 3338 | continue; |
|---|
| 3339 | } |
|---|
| 3340 | |
|---|
| 3341 | unsigned uLabelLen=0; |
|---|
| 3342 | if((uLabelLen=strlen(cLabel))<2) |
|---|
| 3343 | { |
|---|
| 3344 | printf("cLabel is too short\n"); |
|---|
| 3345 | continue; |
|---|
| 3346 | } |
|---|
| 3347 | if(strchr(cLabel,'.')) |
|---|
| 3348 | { |
|---|
| 3349 | printf("cLabel has at least one '.'\n"); |
|---|
| 3350 | continue; |
|---|
| 3351 | } |
|---|
| 3352 | if(strstr(cLabel,"-clone")) |
|---|
| 3353 | { |
|---|
| 3354 | printf("cLabel can't have '-clone'\n"); |
|---|
| 3355 | continue; |
|---|
| 3356 | } |
|---|
| 3357 | unsigned uCreateAppliance=0; |
|---|
| 3358 | char gcIPv4[32]={""}; |
|---|
| 3359 | if(uCreateAppliance) |
|---|
| 3360 | { |
|---|
| 3361 | if(!strstr(cLabel+(uLabelLen-strlen("-app")-1),"-app")) |
|---|
| 3362 | { |
|---|
| 3363 | printf("Appliance cLabel must end with '-app'.\n"); |
|---|
| 3364 | continue; |
|---|
| 3365 | } |
|---|
| 3366 | if(strlen(gcIPv4)<7 || strlen(gcIPv4)>15) |
|---|
| 3367 | { |
|---|
| 3368 | printf("Appliance requires valid gcIPv4.\n"); |
|---|
| 3369 | continue; |
|---|
| 3370 | } |
|---|
| 3371 | |
|---|
| 3372 | unsigned a=0,b=0,c=0,d=0; |
|---|
| 3373 | sscanf(gcIPv4,"%u.%u.%u.%u",&a,&b,&c,&d); |
|---|
| 3374 | if(a==0 || d==0) |
|---|
| 3375 | { |
|---|
| 3376 | printf("Appliance requires valid gcIPv4.\n"); |
|---|
| 3377 | continue; |
|---|
| 3378 | } |
|---|
| 3379 | } |
|---|
| 3380 | else |
|---|
| 3381 | { |
|---|
| 3382 | if(strstr(cLabel,"-app")) |
|---|
| 3383 | { |
|---|
| 3384 | printf("Normal container cLabel can't have '-app'\n"); |
|---|
| 3385 | continue; |
|---|
| 3386 | } |
|---|
| 3387 | } |
|---|
| 3388 | |
|---|
| 3389 | unsigned uHostnameLen=0; |
|---|
| 3390 | if((uHostnameLen=strlen(cHostname))<5) |
|---|
| 3391 | { |
|---|
| 3392 | printf("cHostname is too short\n"); |
|---|
| 3393 | continue; |
|---|
| 3394 | } |
|---|
| 3395 | if(cHostname[uHostnameLen-1]=='.') |
|---|
| 3396 | { |
|---|
| 3397 | printf("cHostname can't end with a '.'\n"); |
|---|
| 3398 | continue; |
|---|
| 3399 | } |
|---|
| 3400 | if(strstr(cHostname+(uHostnameLen-strlen(".cloneNN")-1),".clone")) |
|---|
| 3401 | { |
|---|
| 3402 | printf("cHostname can't end with '.cloneN'\n"); |
|---|
| 3403 | continue; |
|---|
| 3404 | } |
|---|
| 3405 | //New rule: cLabel must be first part (first stop) of cHostname. |
|---|
| 3406 | if(strncmp(cLabel,cHostname,uLabelLen)) |
|---|
| 3407 | { |
|---|
| 3408 | printf("cLabel must be first part of cHostname.\n"); |
|---|
| 3409 | continue; |
|---|
| 3410 | } |
|---|
| 3411 | if(uIPv4==0) |
|---|
| 3412 | { |
|---|
| 3413 | printf("You must select a uIPv4\n"); |
|---|
| 3414 | continue; |
|---|
| 3415 | } |
|---|
| 3416 | |
|---|
| 3417 | //Let's not allow same cLabel containers in our system for now. |
|---|
| 3418 | sprintf(gcQuery,"SELECT uContainer FROM tContainer WHERE cLabel='%s'",cLabel); |
|---|
| 3419 | mysql_query(&gMysql,gcQuery); |
|---|
| 3420 | if(mysql_errno(&gMysql)) |
|---|
| 3421 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3422 | res=mysql_store_result(&gMysql); |
|---|
| 3423 | if(mysql_num_rows(res)>0) |
|---|
| 3424 | { |
|---|
| 3425 | mysql_free_result(res); |
|---|
| 3426 | printf("cLabel already in use\n"); |
|---|
| 3427 | continue; |
|---|
| 3428 | } |
|---|
| 3429 | mysql_free_result(res); |
|---|
| 3430 | |
|---|
| 3431 | char cNCCloneRange[256]={""}; |
|---|
| 3432 | GetDatacenterProp(uDatacenter,"NewContainerCloneRange",cNCCloneRange); |
|---|
| 3433 | if(cNCCloneRange[0] && uIpv4InCIDR4(ForeignKey("tIP","cLabel",uIPv4),cNCCloneRange)) |
|---|
| 3434 | { |
|---|
| 3435 | printf("uIPv4 must not be in datacenter clone IP range\n"); |
|---|
| 3436 | continue; |
|---|
| 3437 | } |
|---|
| 3438 | |
|---|
| 3439 | if(uOSLoopTemplate==0) |
|---|
| 3440 | { |
|---|
| 3441 | printf("You must select a uOSTemplate\n"); |
|---|
| 3442 | continue; |
|---|
| 3443 | } |
|---|
| 3444 | if(uConfig==0) |
|---|
| 3445 | { |
|---|
| 3446 | printf("You must select a uConfig\n"); |
|---|
| 3447 | continue; |
|---|
| 3448 | } |
|---|
| 3449 | if(uNameserver==0) |
|---|
| 3450 | { |
|---|
| 3451 | printf("You must select a uNameserver\n"); |
|---|
| 3452 | continue; |
|---|
| 3453 | } |
|---|
| 3454 | if(uSearchdomain==0) |
|---|
| 3455 | { |
|---|
| 3456 | printf("You must select a uSearchdomain\n"); |
|---|
| 3457 | continue; |
|---|
| 3458 | } |
|---|
| 3459 | |
|---|
| 3460 | if(uGroup==0) |
|---|
| 3461 | { |
|---|
| 3462 | printf("Group is now required\n"); |
|---|
| 3463 | continue; |
|---|
| 3464 | } |
|---|
| 3465 | |
|---|
| 3466 | //DNS sanity check |
|---|
| 3467 | if(uDNSJob) |
|---|
| 3468 | { |
|---|
| 3469 | char cunxsBindARecordJobZone[256]={""}; |
|---|
| 3470 | GetConfiguration("cunxsBindARecordJobZone",cunxsBindARecordJobZone,uDatacenter,0,0,0); |
|---|
| 3471 | if(!cunxsBindARecordJobZone[0]) |
|---|
| 3472 | { |
|---|
| 3473 | printf("Create job for unxsBind," |
|---|
| 3474 | " but no cunxsBindARecordJobZone\n"); |
|---|
| 3475 | continue; |
|---|
| 3476 | } |
|---|
| 3477 | |
|---|
| 3478 | if(!strstr(cHostname+(uHostnameLen-strlen(cunxsBindARecordJobZone)-1),cunxsBindARecordJobZone)) |
|---|
| 3479 | { |
|---|
| 3480 | printf("cHostname must end with cunxsBindARecordJobZone\n"); |
|---|
| 3481 | continue; |
|---|
| 3482 | } |
|---|
| 3483 | } |
|---|
| 3484 | |
|---|
| 3485 | if(cPasswd[0] && strlen(cPasswd)<6) |
|---|
| 3486 | { |
|---|
| 3487 | printf("Optional password must be at least 6 chars\n"); |
|---|
| 3488 | continue; |
|---|
| 3489 | } |
|---|
| 3490 | //Direct datacenter checks |
|---|
| 3491 | unsigned uIPv4Datacenter=0; |
|---|
| 3492 | sscanf(ForeignKey("tIP","uDatacenter",uIPv4),"%u",&uIPv4Datacenter); |
|---|
| 3493 | if(uDatacenter!=uIPv4Datacenter) |
|---|
| 3494 | { |
|---|
| 3495 | printf("The specified uIPv4 does not " |
|---|
| 3496 | "belong to the specified uDatacenter.\n"); |
|---|
| 3497 | continue; |
|---|
| 3498 | } |
|---|
| 3499 | unsigned uNodeDatacenter=0; |
|---|
| 3500 | sscanf(ForeignKey("tNode","uDatacenter",uNode),"%u",&uNodeDatacenter); |
|---|
| 3501 | if(uDatacenter!=uNodeDatacenter) |
|---|
| 3502 | { |
|---|
| 3503 | printf("The specified uNode does not " |
|---|
| 3504 | "belong to the specified uDatacenter.\n"); |
|---|
| 3505 | continue; |
|---|
| 3506 | } |
|---|
| 3507 | |
|---|
| 3508 | //If auto clone setup check required values |
|---|
| 3509 | char cAutoCloneNode[256]={""}; |
|---|
| 3510 | GetConfiguration("cAutoCloneNode",cAutoCloneNode,uDatacenter,0,0,0); |
|---|
| 3511 | if(cAutoCloneNode[0]) |
|---|
| 3512 | { |
|---|
| 3513 | if(uCloneTargetNode==0) |
|---|
| 3514 | { |
|---|
| 3515 | printf("Please select a valid target node" |
|---|
| 3516 | " for the clone\n"); |
|---|
| 3517 | continue; |
|---|
| 3518 | } |
|---|
| 3519 | if(uCloneTargetNode==uNode) |
|---|
| 3520 | { |
|---|
| 3521 | printf("Can't clone to same node\n"); |
|---|
| 3522 | continue; |
|---|
| 3523 | } |
|---|
| 3524 | |
|---|
| 3525 | GetNodeProp(uCloneTargetNode,"NewContainerMode",cNCMNode); |
|---|
| 3526 | if(cNCMNode[0] && strcmp(cNCMNode,"Clone")) |
|---|
| 3527 | { |
|---|
| 3528 | printf("Selected clone target node is not configured for clone containers." |
|---|
| 3529 | "Select another.\n"); |
|---|
| 3530 | continue; |
|---|
| 3531 | } |
|---|
| 3532 | |
|---|
| 3533 | sscanf(ForeignKey("tNode","uDatacenter",uCloneTargetNode),"%u",&uNodeDatacenter); |
|---|
| 3534 | if(uDatacenter!=uNodeDatacenter) |
|---|
| 3535 | { |
|---|
| 3536 | printf("The specified clone uNode does not " |
|---|
| 3537 | "belong to the specified uDatacenter.\n"); |
|---|
| 3538 | continue; |
|---|
| 3539 | } |
|---|
| 3540 | |
|---|
| 3541 | char cTargetNodeIPv4[256]={""}; |
|---|
| 3542 | GetNodeProp(uCloneTargetNode,"cIPv4",cTargetNodeIPv4); |
|---|
| 3543 | if(!cTargetNodeIPv4[0]) |
|---|
| 3544 | { |
|---|
| 3545 | printf("Your target node is" |
|---|
| 3546 | " missing it's cIPv4 property\n"); |
|---|
| 3547 | continue; |
|---|
| 3548 | } |
|---|
| 3549 | if(!uCloneIPv4) |
|---|
| 3550 | { |
|---|
| 3551 | printf("You must select an IP for the clone\n"); |
|---|
| 3552 | continue; |
|---|
| 3553 | } |
|---|
| 3554 | if(uCloneIPv4==uIPv4) |
|---|
| 3555 | { |
|---|
| 3556 | printf("You must select a different IP for the" |
|---|
| 3557 | " clone\n"); |
|---|
| 3558 | continue; |
|---|
| 3559 | } |
|---|
| 3560 | sscanf(ForeignKey("tIP","uDatacenter",uCloneIPv4),"%u",&uIPv4Datacenter); |
|---|
| 3561 | if(uDatacenter!=uIPv4Datacenter) |
|---|
| 3562 | { |
|---|
| 3563 | printf("The specified uIPv4 does not " |
|---|
| 3564 | "belong to the specified uDatacenter.\n"); |
|---|
| 3565 | continue; |
|---|
| 3566 | } |
|---|
| 3567 | if(cNCCloneRange[0] && !uIpv4InCIDR4(ForeignKey("tIP","cLabel",uCloneIPv4),cNCCloneRange)) |
|---|
| 3568 | { |
|---|
| 3569 | printf("Clone start uIPv4 must be in datacenter clone IP range\n"); |
|---|
| 3570 | continue; |
|---|
| 3571 | } |
|---|
| 3572 | if(uSyncPeriod>86400*30 || (uSyncPeriod && uSyncPeriod<300)) |
|---|
| 3573 | { |
|---|
| 3574 | printf("Clone uSyncPeriod out of range:" |
|---|
| 3575 | " Max 30 days, min 5 minutes or 0 off.\n"); |
|---|
| 3576 | continue; |
|---|
| 3577 | } |
|---|
| 3578 | }//if(cAutoCloneNode[0]) |
|---|
| 3579 | |
|---|
| 3580 | //TODO review this policy. |
|---|
| 3581 | //No same names or hostnames for same datacenter allowed. |
|---|
| 3582 | //TODO periods "." should be expanded to "[.]" |
|---|
| 3583 | //for correct cHostname REGEXP. |
|---|
| 3584 | sprintf(gcQuery,"SELECT uContainer FROM tContainer WHERE (" |
|---|
| 3585 | " cHostname REGEXP '^%s[0-9]+%s$'" |
|---|
| 3586 | " OR cLabel REGEXP '%s[0-9]+$'" |
|---|
| 3587 | " ) AND uDatacenter=%u LIMIT 1", |
|---|
| 3588 | cLabel,cHostname,cLabel,uDatacenter); |
|---|
| 3589 | mysql_query(&gMysql,gcQuery); |
|---|
| 3590 | if(mysql_errno(&gMysql)) |
|---|
| 3591 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3592 | res=mysql_store_result(&gMysql); |
|---|
| 3593 | if(mysql_num_rows(res)>0) |
|---|
| 3594 | { |
|---|
| 3595 | mysql_free_result(res); |
|---|
| 3596 | printf("Similar cHostname cLabel pattern already used at this datacenter!\n"); |
|---|
| 3597 | continue; |
|---|
| 3598 | } |
|---|
| 3599 | mysql_free_result(res); |
|---|
| 3600 | |
|---|
| 3601 | // |
|---|
| 3602 | //Everything is ready and checked so we start modifying things |
|---|
| 3603 | // |
|---|
| 3604 | unsigned uContainer=0; |
|---|
| 3605 | sprintf(gcQuery,"INSERT INTO tContainer SET cLabel='%s',cHostname='%s'," |
|---|
| 3606 | "uIPv4=%u," |
|---|
| 3607 | "uDatacenter=%u," |
|---|
| 3608 | "uNode=%u," |
|---|
| 3609 | "uOSTemplate=%u," |
|---|
| 3610 | "uConfig=%u," |
|---|
| 3611 | "uNameserver=%u," |
|---|
| 3612 | "uSearchdomain=%u," |
|---|
| 3613 | "uStatus=%u," |
|---|
| 3614 | "uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 3615 | cLabel,cHostname, |
|---|
| 3616 | uIPv4, |
|---|
| 3617 | uDatacenter, |
|---|
| 3618 | uNode, |
|---|
| 3619 | uOSLoopTemplate, |
|---|
| 3620 | uConfig, |
|---|
| 3621 | uNameserver, |
|---|
| 3622 | uSearchdomain, |
|---|
| 3623 | uINITSETUP, |
|---|
| 3624 | uOwner); |
|---|
| 3625 | mysql_query(&gMysql,gcQuery); |
|---|
| 3626 | if(mysql_errno(&gMysql)) |
|---|
| 3627 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3628 | uContainer=mysql_insert_id(&gMysql); |
|---|
| 3629 | if(!uContainer) |
|---|
| 3630 | { |
|---|
| 3631 | printf("uContainer not determined!!"); |
|---|
| 3632 | continue; |
|---|
| 3633 | } |
|---|
| 3634 | |
|---|
| 3635 | //Add to group |
|---|
| 3636 | if(uGroup) |
|---|
| 3637 | ChangeGroup(uContainer,uGroup); |
|---|
| 3638 | |
|---|
| 3639 | //tIP |
|---|
| 3640 | sprintf(gcQuery,"UPDATE tIP SET uAvailable=0" |
|---|
| 3641 | " WHERE uIP=%u AND uAvailable=1 AND uDatacenter=%u", |
|---|
| 3642 | uIPv4,uDatacenter); |
|---|
| 3643 | mysql_query(&gMysql,gcQuery); |
|---|
| 3644 | if(mysql_errno(&gMysql)) |
|---|
| 3645 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3646 | if(mysql_affected_rows(&gMysql)!=1) |
|---|
| 3647 | { |
|---|
| 3648 | sprintf(gcQuery,"DELETE FROM tContainer WHERE uContainer=%u", |
|---|
| 3649 | uContainer); |
|---|
| 3650 | mysql_query(&gMysql,gcQuery); |
|---|
| 3651 | if(mysql_errno(&gMysql)) |
|---|
| 3652 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3653 | printf("Someone grabbed your IP container creation aborted!"); |
|---|
| 3654 | continue; |
|---|
| 3655 | } |
|---|
| 3656 | |
|---|
| 3657 | //Add properties |
|---|
| 3658 | //Name property |
|---|
| 3659 | sprintf(gcQuery,"INSERT INTO tProperty SET uKey=%u,uType=3" |
|---|
| 3660 | ",uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())" |
|---|
| 3661 | ",cName='Name',cValue='%s'", |
|---|
| 3662 | uContainer,uOwner,cLabel); |
|---|
| 3663 | mysql_query(&gMysql,gcQuery); |
|---|
| 3664 | if(mysql_errno(&gMysql)) |
|---|
| 3665 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3666 | //cPasswd property |
|---|
| 3667 | sprintf(gcQuery,"INSERT INTO tProperty SET uKey=%u,uType=3" |
|---|
| 3668 | ",uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())" |
|---|
| 3669 | ",cName='cPasswd',cValue='%s'", |
|---|
| 3670 | uContainer,uOwner,cPasswd); |
|---|
| 3671 | mysql_query(&gMysql,gcQuery); |
|---|
| 3672 | if(mysql_errno(&gMysql)) |
|---|
| 3673 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3674 | //Optional timezone note the --- not selected value. |
|---|
| 3675 | if(cTimeZone[0]) |
|---|
| 3676 | { |
|---|
| 3677 | sprintf(gcQuery,"INSERT INTO tProperty SET cName='cOrg_TimeZone',cValue='%s',uType=3,uKey=%u" |
|---|
| 3678 | ",uOwner=%u,uCreatedBy=1,uCreatedDate=UNIX_TIMESTAMP(NOW())", |
|---|
| 3679 | cTimeZone,uContainer,uOwner); |
|---|
| 3680 | mysql_query(&gMysql,gcQuery); |
|---|
| 3681 | if(mysql_errno(&gMysql)) |
|---|
| 3682 | htmlPlainTextError(mysql_error(&gMysql)); |
|---|
| 3683 | } |
|---|
| 3684 | |
|---|
| 3685 | if(cAutoCloneNode[0]) |
|---|
| 3686 | { |
|---|
| 3687 | unsigned uNewVeid=0; |
|---|
| 3688 | unsigned uStatus=uINITSETUP; |
|---|
| 3689 | |
|---|
| 3690 | uNewVeid=CommonCloneContainer( uContainer, |
|---|
| 3691 | uOSLoopTemplate, |
|---|
| 3692 | uConfig, |
|---|
| 3693 | uNameserver, |
|---|
| 3694 | uSearchdomain, |
|---|
| 3695 | uDatacenter, |
|---|
| 3696 | uDatacenter, |
|---|
| 3697 | uOwner, |
|---|
| 3698 | cLabel, |
|---|
| 3699 | uNode, |
|---|
| 3700 | uStatus, |
|---|
| 3701 | cHostname, |
|---|
| 3702 | cIPv4CloneClassC, |
|---|
| 3703 | uCloneIPv4, |
|---|
| 3704 | cCloneLabel, |
|---|
| 3705 | cCloneHostname, |
|---|
| 3706 | uCloneTargetNode, |
|---|
| 3707 | uSyncPeriod, |
|---|
| 3708 | 1, |
|---|
| 3709 | uCloneStopped,7); |
|---|
| 3710 | if(!uNewVeid) |
|---|
| 3711 | { |
|---|
| 3712 | printf("Clone container %s creation failed!",cCloneHostname); |
|---|
| 3713 | } |
|---|
| 3714 | else |
|---|
| 3715 | { |
|---|
| 3716 | SetContainerStatus(uContainer,uINITSETUP);//See CommonCloneContainer() |
|---|
| 3717 | if(uGroup) |
|---|
| 3718 | ChangeGroup(uNewVeid,uGroup); |
|---|
| 3719 | } |
|---|
| 3720 | }//cAutoCloneNode |
|---|
| 3721 | |
|---|
| 3722 | if(uDNSJob) |
|---|
| 3723 | CreateDNSJob(uIPv4,uOwner,NULL,cHostname,uDatacenter,1); |
|---|
| 3724 | |
|---|
| 3725 | }//valid hostname and label |
|---|
| 3726 | } |
|---|
| 3727 | } |
|---|
| 3728 | |
|---|
| 3729 | |
|---|
| 3730 | fclose(fp); |
|---|
| 3731 | |
|---|
| 3732 | printf("\nMassCreateContainers(): End\n"); |
|---|
| 3733 | |
|---|
| 3734 | }//void MassCreateContainers(char *cConfigfileName) |
|---|