void MainWindow::save() { QString filename = QFileDialog::getSaveFileName(this); if (!filename.isEmpty()) { rapidjson::Document config_doc = createConfigDoc(); try { writeConfigDocToFile(config_doc, filename.toStdString()); statusBar()->showMessage(tr("File saved"), 2000); } catch (std::exception &e) { std::cerr << "error: " << e.what() << std::endl; QString message_title("Error"); QString message_text(e.what()); QMessageBox::critical(this, message_title, message_text); setEnabled(true); } } }
void MainWindow::solveFEA() { rapidjson::Document configDoc = createConfigDoc(); feaTerminated = false; try { writeConfigDocToFile(configDoc, feaTmpConfigFilename); QStringList feaProgramArgs; feaProgramArgs << "-c" << feaTmpConfigFilename.c_str(); feaProcess = new QProcess(this); connect(progress, SIGNAL(canceled()), this, SLOT(handleCanceledFEA())); connect(feaProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleFinishedFEA(int, QProcess::ExitStatus))); connect(feaProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(updateProgressText())); connect(feaProcess, SIGNAL(readyReadStandardError()), this, SLOT(handleCanceledFEA())); feaProcess->start(QString::fromStdString(feaProgram), feaProgramArgs); } catch (std::exception &e) { std::cerr << "error: " << e.what(); QString message_title("Error"); QString message_text(e.what()); QMessageBox::critical(this, message_title, message_text); setEnabled(true); } }
bool MainWindow::checkFilesReady() { int errorCounter = 0; QString message_text(""); if(!checkFileOpens(feaProgram)){ ++errorCounter; message_text.append("Unable to find command line application `fea_gui`.\n" "The command line application should be in the same" "directory as the gui.\n\n"); } if (nodesLineEdit->displayText().isEmpty()) { ++errorCounter; message_text.append("No file for nodes selected.\n"); } else { std::string filename = nodesLineEdit->displayText().toStdString(); if (!checkFileOpens(filename)) { ++errorCounter; message_text.append("Unable to open file selected for nodes.\n"); } } if (elemsLineEdit->displayText().isEmpty()) { ++errorCounter; message_text.append("No file for elements selected.\n"); } else { std::string filename = elemsLineEdit->displayText().toStdString(); if (!checkFileOpens(filename)) { ++errorCounter; message_text.append("Unable to open file selected for elements.\n"); } } if (propsLineEdit->displayText().isEmpty()) { ++errorCounter; message_text.append("No file for properties selected.\n"); } else { std::string filename = propsLineEdit->displayText().toStdString(); if (!checkFileOpens(filename)) { ++errorCounter; message_text.append("Unable to open file selected for properties.\n"); } } if (bcsLineEdit->displayText().isEmpty() && forcesLineEdit->displayText().isEmpty()) { ++errorCounter; message_text.append("No prescribed boundary conditions or forces.\n"); } if (!bcsLineEdit->displayText().isEmpty()) { std::string filename = bcsLineEdit->displayText().toStdString(); if (!checkFileOpens(filename)) { ++errorCounter; message_text.append("Unable to open file selected for boundary conditions.\n"); } } if (!forcesLineEdit->displayText().isEmpty()) { std::string filename = forcesLineEdit->displayText().toStdString(); if (!checkFileOpens(filename)) { ++errorCounter; message_text.append("Unable to open file selected for forces.\n"); } } if (!tiesLineEdit->displayText().isEmpty()) { std::string filename = tiesLineEdit->displayText().toStdString(); if (!checkFileOpens(filename)) { ++errorCounter; message_text.append("Unable to open file selected for ties.\n"); } } bool isReady = true; if(errorCounter > 0) { QString message_title(tr("%1 Error(s) found.").arg(QString::number(errorCounter))); QMessageBox::critical(this, message_title, message_text); isReady = false; } return isReady; }
void send_alert(activealerts_t *alert, FILE *logfd) { recip_t *recip; int first = 1; int alertcount = 0; time_t now = getcurrenttime(NULL); /* A_PAGING, A_NORECIP, A_ACKED, A_RECOVERED, A_DISABLED, A_NOTIFY, A_DEAD */ char *alerttxt[A_DEAD+1] = { "Paging", "Norecip", "Acked", "Recovered", "Disabled", "Notify", "Dead" }; dbgprintf("send_alert %s:%s state %d\n", alert->hostname, alert->testname, (int)alert->state); traceprintf("send_alert %s:%s state %s\n", alert->hostname, alert->testname, alerttxt[alert->state]); stoprulefound = 0; while (!stoprulefound && ((recip = next_recipient(alert, &first, NULL, NULL)) != NULL)) { /* If this is an "UNMATCHED" rule, ignore it if we have already sent out some alert */ if (recip->unmatchedonly && (alertcount != 0)) { traceprintf("Recipient '%s' dropped, not unmatched (count=%d)\n", recip->recipient, alertcount); continue; } if (recip->noalerts && ((alert->state == A_PAGING) || (alert->state == A_RECOVERED) || (alert->state == A_DISABLED))) { traceprintf("Recipient '%s' dropped (NOALERT)\n", recip->recipient); continue; } if (recip->method == M_IGNORE) { traceprintf("IGNORE rule found\n"); continue; } if (alert->state == A_PAGING) { repeat_t *rpt = NULL; /* * This runs in a child-process context, so the record we * might create here is NOT used later on. */ rpt = find_repeatinfo(alert, recip, 1); if (!rpt) continue; /* Happens for e.g. M_IGNORE recipients */ dbgprintf(" repeat %s at %d\n", rpt->recipid, rpt->nextalert); if (rpt->nextalert > now) { traceprintf("Recipient '%s' dropped, next alert due at %ld > %ld\n", rpt->recipid, (long)rpt->nextalert, (long)now); continue; } alertcount++; } else if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { /* RECOVERED messages require that we've sent out an alert before */ repeat_t *rpt = NULL; rpt = find_repeatinfo(alert, recip, 0); if (!rpt) continue; alertcount++; } dbgprintf(" Alert for %s:%s to %s\n", alert->hostname, alert->testname, recip->recipient); switch (recip->method) { case M_IGNORE: break; case M_MAIL: { char cmd[32768]; char *mailsubj; char *mailrecip; FILE *mailpipe; MEMDEFINE(cmd); mailsubj = message_subject(alert, recip); mailrecip = message_recipient(recip->recipient, alert->hostname, alert->testname, colorname(alert->color)); if (mailsubj) { if (xgetenv("MAIL")) sprintf(cmd, "%s \"%s\" ", xgetenv("MAIL"), mailsubj); else if (xgetenv("MAILC")) sprintf(cmd, "%s -s \"%s\" ", xgetenv("MAILC"), mailsubj); else sprintf(cmd, "mail -s \"%s\" ", mailsubj); } else { if (xgetenv("MAILC")) sprintf(cmd, "%s ", xgetenv("MAILC")); else sprintf(cmd, "mail "); } strcat(cmd, mailrecip); traceprintf("Mail alert with command '%s'\n", cmd); if (testonly) { MEMUNDEFINE(cmd); break; } mailpipe = popen(cmd, "w"); if (mailpipe) { fprintf(mailpipe, "%s", message_text(alert, recip)); pclose(mailpipe); if (logfd) { init_timestamp(); fprintf(logfd, "%s %s.%s (%s) %s[%d] %ld %d", timestamp, alert->hostname, alert->testname, alert->ip, mailrecip, recip->cfid, (long)now, servicecode(alert->testname)); if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { fprintf(logfd, " %ld\n", (long)(now - alert->eventstart)); } else { fprintf(logfd, "\n"); } fflush(logfd); } } else { errprintf("ERROR: Cannot open command pipe for '%s' - alert lost!\n", cmd); traceprintf("Mail pipe failed - alert lost\n"); } MEMUNDEFINE(cmd); } break; case M_SCRIPT: { pid_t scriptpid; char *scriptrecip; traceprintf("Script alert with command '%s' and recipient %s\n", recip->scriptname, recip->recipient); if (testonly) break; scriptrecip = message_recipient(recip->recipient, alert->hostname, alert->testname, colorname(alert->color)); scriptpid = fork(); if (scriptpid == 0) { /* Setup all of the environment for a paging script */ void *hinfo; char *p; int ip1=0, ip2=0, ip3=0, ip4=0; char *bbalphamsg, *ackcode, *rcpt, *bbhostname, *bbhostsvc, *bbhostsvccommas, *bbnumeric, *machip, *bbsvcname, *bbsvcnum, *bbcolorlevel, *recovered, *downsecs, *eventtstamp, *downsecsmsg, *cfidtxt; char *alertid, *alertidenv; int msglen; cfidtxt = (char *)malloc(strlen("CFID=") + 10); sprintf(cfidtxt, "CFID=%d", recip->cfid); putenv(cfidtxt); p = message_text(alert, recip); msglen = strlen(p); if (msglen > MAX_ALERTMSG_SCRIPTS) { dbgprintf("Cropping large alert message from %d to %d bytes\n", msglen, MAX_ALERTMSG_SCRIPTS); msglen = MAX_ALERTMSG_SCRIPTS; } msglen += strlen("BBALPHAMSG="); bbalphamsg = (char *)malloc(msglen + 1); snprintf(bbalphamsg, msglen+1, "BBALPHAMSG=%s", p); putenv(bbalphamsg); ackcode = (char *)malloc(strlen("ACKCODE=") + 10); sprintf(ackcode, "ACKCODE=%d", alert->cookie); putenv(ackcode); rcpt = (char *)malloc(strlen("RCPT=") + strlen(scriptrecip) + 1); sprintf(rcpt, "RCPT=%s", scriptrecip); putenv(rcpt); bbhostname = (char *)malloc(strlen("BBHOSTNAME=") + strlen(alert->hostname) + 1); sprintf(bbhostname, "BBHOSTNAME=%s", alert->hostname); putenv(bbhostname); bbhostsvc = (char *)malloc(strlen("BBHOSTSVC=") + strlen(alert->hostname) + 1 + strlen(alert->testname) + 1); sprintf(bbhostsvc, "BBHOSTSVC=%s.%s", alert->hostname, alert->testname); putenv(bbhostsvc); bbhostsvccommas = (char *)malloc(strlen("BBHOSTSVCCOMMAS=") + strlen(alert->hostname) + 1 + strlen(alert->testname) + 1); sprintf(bbhostsvccommas, "BBHOSTSVCCOMMAS=%s.%s", commafy(alert->hostname), alert->testname); putenv(bbhostsvccommas); bbnumeric = (char *)malloc(strlen("BBNUMERIC=") + 22 + 1); p = bbnumeric; p += sprintf(p, "BBNUMERIC="); p += sprintf(p, "%03d", servicecode(alert->testname)); sscanf(alert->ip, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4); p += sprintf(p, "%03d%03d%03d%03d", ip1, ip2, ip3, ip4); p += sprintf(p, "%d", alert->cookie); putenv(bbnumeric); machip = (char *)malloc(strlen("MACHIP=") + 13); sprintf(machip, "MACHIP=%03d%03d%03d%03d", ip1, ip2, ip3, ip4); putenv(machip); bbsvcname = (char *)malloc(strlen("BBSVCNAME=") + strlen(alert->testname) + 1); sprintf(bbsvcname, "BBSVCNAME=%s", alert->testname); putenv(bbsvcname); bbsvcnum = (char *)malloc(strlen("BBSVCNUM=") + 10); sprintf(bbsvcnum, "BBSVCNUM=%d", servicecode(alert->testname)); putenv(bbsvcnum); bbcolorlevel = (char *)malloc(strlen("BBCOLORLEVEL=") + strlen(colorname(alert->color)) + 1); sprintf(bbcolorlevel, "BBCOLORLEVEL=%s", colorname(alert->color)); putenv(bbcolorlevel); recovered = (char *)malloc(strlen("RECOVERED=") + 2); switch (alert->state) { case A_RECOVERED: strcpy(recovered, "RECOVERED=1"); break; case A_DISABLED: strcpy(recovered, "RECOVERED=2"); break; default: strcpy(recovered, "RECOVERED=0"); break; } putenv(recovered); downsecs = (char *)malloc(strlen("DOWNSECS=") + 20); sprintf(downsecs, "DOWNSECS=%ld", (long)(getcurrenttime(NULL) - alert->eventstart)); putenv(downsecs); eventtstamp = (char *)malloc(strlen("EVENTSTART=") + 20); sprintf(eventtstamp, "EVENTSTART=%ld", (long)alert->eventstart); putenv(eventtstamp); if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { downsecsmsg = (char *)malloc(strlen("DOWNSECSMSG=Event duration :") + 20); sprintf(downsecsmsg, "DOWNSECSMSG=Event duration : %ld", (long)(getcurrenttime(NULL) - alert->eventstart)); } else { downsecsmsg = strdup("DOWNSECSMSG="); } putenv(downsecsmsg); alertid = make_alertid(alert->hostname, alert->testname, alert->eventstart); alertidenv = (char *)malloc(strlen("ALERTID=") + strlen(alertid) + 10); sprintf(alertidenv, "ALERTID=%s", alertid); putenv(alertidenv); hinfo = hostinfo(alert->hostname); if (hinfo) { enum xmh_item_t walk; char *itm, *id, *bbhenv; for (walk = 0; (walk < XMH_LAST); walk++) { itm = xmh_item(hinfo, walk); id = xmh_item_id(walk); if (itm && id) { bbhenv = (char *)malloc(strlen(id) + strlen(itm) + 2); sprintf(bbhenv, "%s=%s", id, itm); putenv(bbhenv); } } } /* The child starts the script */ execlp(recip->scriptname, recip->scriptname, NULL); errprintf("Could not launch paging script %s: %s\n", recip->scriptname, strerror(errno)); exit(0); } else if (scriptpid > 0) { /* Parent waits for child to complete */ int childstat; wait(&childstat); if (WIFEXITED(childstat) && (WEXITSTATUS(childstat) != 0)) { errprintf("Paging script %s terminated with status %d\n", recip->scriptname, WEXITSTATUS(childstat)); } else if (WIFSIGNALED(childstat)) { errprintf("Paging script %s terminated by signal %d\n", recip->scriptname, WTERMSIG(childstat)); } if (logfd) { init_timestamp(); fprintf(logfd, "%s %s.%s (%s) %s %ld %d", timestamp, alert->hostname, alert->testname, alert->ip, scriptrecip, (long)now, servicecode(alert->testname)); if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { fprintf(logfd, " %ld\n", (long)(now - alert->eventstart)); } else { fprintf(logfd, "\n"); } fflush(logfd); } } else { errprintf("ERROR: Fork failed to launch script '%s' - alert lost\n", recip->scriptname); traceprintf("Script fork failed - alert lost\n"); } } break; } } }