void gen_blessing_list_xml() { FILE *fp; char curfile[256]; char xml_buf[256]; char url_buf[256]; int i; sprintf(curfile, "xml/%s.xml", myfile[4]); if ((fp = fopen(curfile, "w")) != NULL) { fprintf(fp, "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"); fprintf(fp, "<hotsubjects>\n"); for (i = 0; i < topnum; i++) { fprintf(fp, "<hotsubject>\n"); fprintf(fp, "<title>%s</title>\n", encode_url(url_buf,encode_xml(xml_buf, top[i].title, sizeof(xml_buf)),sizeof(url_buf))); fprintf(fp, "<author>%s</author>\n", encode_url(url_buf,top[i].userid,sizeof(url_buf))); fprintf(fp, "<board>%s</board>\n", encode_url(url_buf,top[i].board,sizeof(url_buf))); fprintf(fp, "<time>%ld</time>\n", top[i].date); fprintf(fp, "<number>%d</number>\n", top[i].number); fprintf(fp, "<groupid>%d</groupid>\n", top[i].groupid); fprintf(fp, "</hotsubject>\n"); } fprintf(fp, "</hotsubjects>\n"); fclose(fp); } }
void gen_board_rank_xml() { int i; FILE *fp; char xmlfile[STRLEN]; char xml_buf[256]; char url_buf[256]; const struct boardheader *bp; int sec_id; snprintf(xmlfile, sizeof(xmlfile), BBSHOME "/xml/board.xml"); if ((fp = fopen(xmlfile, "w")) == NULL) return; fprintf(fp, "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"); fprintf(fp, "<BoardList Desc=\"%s\">\n",encode_url(url_buf,"讨论区使用状况统计",sizeof(url_buf))); for (i = 0; i < n; i++) { bp = getbcache(x[i].filename); if (bp == NULL || (bp->flag & BOARD_GROUP)) continue; if ((sec_id = get_seccode_index(bp->title[0])) < 0) continue; fprintf(fp, "<Board>\n"); fprintf(fp, "<EnglishName>%s</EnglishName>\n", encode_url(url_buf,encode_xml(xml_buf, x[i].filename, sizeof(xml_buf)),sizeof(url_buf))); fprintf(fp, "<ChineseName>%s</ChineseName>\n", encode_url(url_buf,encode_xml(xml_buf, x[i].title, sizeof(xml_buf)),sizeof(url_buf))); fprintf(fp, "<Online>%d</Online>\n", x[i].online); fprintf(fp, "<Articles>%d</Articles>\n", x[i].nowid-x[i].yesid); fprintf(fp, "<SecId>%d</SecId>\n", sec_id); fprintf(fp, "</Board>\n"); } fprintf(fp, "</BoardList>\n"); fclose(fp); }
void gen_sec_hot_subjects_xml(int mytype, int secid) { FILE *fp; char curfile[256]; char xml_buf[256]; char url_buf[256]; int i; sprintf(curfile, "xml/%s_sec%d.xml", myfile[mytype], secid); if ((fp = fopen(curfile, "w")) != NULL) { fprintf(fp, "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"); fprintf(fp, "<hotsubjects>\n"); for (i = 0; i < sectopnum[secid]; i++) { fprintf(fp, "<hotsubject>\n"); fprintf(fp, "<title>%s</title>\n", encode_url(url_buf,encode_xml(xml_buf, sectop[secid][i].title, sizeof(xml_buf)),sizeof(url_buf))); fprintf(fp, "<author>%s</author>\n", encode_url(url_buf,sectop[secid][i].userid,sizeof(url_buf))); fprintf(fp, "<board>%s</board>\n", encode_url(url_buf,sectop[secid][i].board,sizeof(url_buf))); fprintf(fp, "<time>%ld</time>\n", sectop[secid][i].date); fprintf(fp, "<number>%d</number>\n", sectop[secid][i].number); fprintf(fp, "<groupid>%d</groupid>\n", sectop[secid][i].groupid); fprintf(fp, "</hotsubject>\n"); } fprintf(fp, "</hotsubjects>\n"); fclose(fp); } }
int gen_newboards_xml(struct newboarddata *boarddata) { FILE *fp = NULL; int i; char title[STRLEN]; char buf[128]; if ((fp = fopen("xml/newboards.xml", "w")) == NULL) { printf("open xml/newboards.xml file failed.\n"); return -1; } printf("boardcount=%d\n", boarddata->nBoards); fprintf(fp, "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"); fprintf(fp, "<newboards>\n"); for(i = 0; i < boarddata->nBoards; i++) { fprintf(fp, "<newboard>\n"); fprintf(fp, "<filename>%s</filename>\n",encode_url(buf, boarddata->boards[i]->filename, sizeof(buf))); strcpy(title, boarddata->boards[i]->title); title[7] = 0; fprintf(fp, "<class>%s</class>\n", encode_url(buf, title, sizeof(buf))); fprintf(fp, "<title>%s</title>\n", encode_url(buf, title+13, sizeof(buf))); fprintf(fp, "<BM>%s</BM>\n", encode_url(buf, boarddata->boards[i]->BM, sizeof(buf))); fprintf(fp, "<count>%d</count>\n", -1); fprintf(fp, "</newboard>\n"); } fprintf(fp, "</newboards>\n"); fclose(fp); return boarddata->nBoards; }
int main(int argc, char *argv[]) { FILE *pipe; int args = 0; char buffer[256] = { 0 }; char url_buffer[4096] = { 0 }; char encoded_url_buffer[4096] = { 0 }; conf_t conf = { { 0 }, { 0 }, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; CURL *curl; CURLcode res; // check if debug is ON if(argc > 1) { if(strcmp(argv[1], "--debug") == 0) { DEBUG = 1; } } // get database configuration if(read_config(&conf)) { return 1; } // format beginning of URL strcat(encoded_url_buffer, conf.url); strcat(encoded_url_buffer, "?"); if(DEBUG) { printf("System Info:\n\n"); } if(conf.cpu_cores) { pipe = popen("cat /proc/cpuinfo | grep processor | wc -l", "r"); fgets(buffer, 256, pipe); buffer[strlen(buffer) - 1] = 0; strcat(url_buffer, "cpu_cores="); strcat(url_buffer, buffer); args = 1; if(DEBUG) printf("cpu_cores = %s\n", buffer); fclose(pipe); } if(conf.cpu_load) { // save top output to tmp file pipe = popen("top -n 2 > /tmp/mor_top_tmp.txt", "r"); // wait for 2 iterations to complete sleep(5); // read tmp file pipe = popen("cat /tmp/mor_top_tmp.txt | grep 'Cpu(s)' | tail -n+2 | sed 's/.*, *\\([0-9.]*\\)%\\id.*/\\1/' | awk '{print 100 - $5}'", "r"); fgets(buffer, 256, pipe); buffer[strlen(buffer) - 1] = 0; if(args) strcat(url_buffer, "&"); strcat(url_buffer, "cpu_load="); strcat(url_buffer, buffer); args = 1; if(DEBUG) printf("cpu_load = %s\n", buffer); fclose(pipe); // remove tpm file pipe = popen("rm -fr /tmp/mor_top_tmp.txt", "r"); fclose(pipe); } if(conf.ram_total || conf.ram_free || conf.ram_used || conf.ram_buffers) { char var1[256]; char var2[256]; char var3[256]; char var4[256]; pipe = popen("free -k | grep -Po '\\d+' | head -n 5", "r"); fscanf(pipe, "%s %s %s %s %s", var1, var2, var3, var4, var4); if(conf.ram_total) { if(args) strcat(url_buffer, "&"); strcat(url_buffer, "ram_total="); strcat(url_buffer, var1); args = 1; if(DEBUG) printf("ram_total = %s\n", var1); } if(conf.ram_used) { if(args) strcat(url_buffer, "&"); strcat(url_buffer, "ram_used="); strcat(url_buffer, var2); args = 1; if(DEBUG) printf("ram_used = %s\n", var2); } if(conf.ram_free) { if(args) strcat(url_buffer, "&"); strcat(url_buffer, "ram_free="); strcat(url_buffer, var3); args = 1; if(DEBUG) printf("ram_free = %s\n", var3); } if(conf.ram_buffers) { if(args) strcat(url_buffer, "&"); strcat(url_buffer, "ram_buffers="); strcat(url_buffer, var4); args = 1; if(DEBUG) printf("ram_buffers = %s\n", var4); } fclose(pipe); } if(conf.mysql) { pipe = popen("service mysqld status | grep -o 'running'", "r");; fgets(buffer, 256, pipe); buffer[strlen(buffer) - 1] = 0; if(args) strcat(url_buffer, "&"); strcat(url_buffer, "mysql="); if(strcmp(buffer, "running") == 0) { strcat(url_buffer, "1"); if(DEBUG) printf("mysql = 1\n"); } else { strcat(url_buffer, "0"); if(DEBUG) printf("mysql = 0\n"); } args = 1; fclose(pipe); } if(conf.httpd) { pipe = popen("service httpd status | grep -o 'running'", "r");; fgets(buffer, 256, pipe); buffer[strlen(buffer) - 1] = 0; if(args) strcat(url_buffer, "&"); strcat(url_buffer, "httpd="); if(strcmp(buffer, "running") == 0) { strcat(url_buffer, "1"); if(DEBUG) printf("httpd = 1\n"); } else { strcat(url_buffer, "0"); if(DEBUG) printf("httpd = 0\n"); } args = 1; fclose(pipe); } if(conf.asterisk) { pipe = popen("service asterisk status | grep -o 'running'", "r");; fgets(buffer, 256, pipe); buffer[strlen(buffer) - 1] = 0; if(args) strcat(url_buffer, "&"); strcat(url_buffer, "asterisk="); if(strcmp(buffer, "running") == 0) { strcat(url_buffer, "1"); if(DEBUG) printf("asterisk = 1\n"); } else { strcat(url_buffer, "0"); if(DEBUG) printf("asterisk = 0\n"); } args = 1; fclose(pipe); } if(conf.hdd) { char pipe_buffer[256] = { 0 }; sprintf(pipe_buffer, "df -h %s | grep -Po '[0-9]+%%' | sed 's|%%||'", conf.file_system); pipe = popen(pipe_buffer, "r"); fgets(buffer, 256, pipe); buffer[strlen(buffer) - 1] = 0; if(args) strcat(url_buffer, "&"); strcat(url_buffer, "hdd="); strcat(url_buffer, buffer); if(DEBUG) printf("hdd = %s\n", buffer); args = 1; fclose(pipe); } if(args) { encode_url(url_buffer, encoded_url_buffer); if(DEBUG) { printf("\nHTTP Request:\n\n"); printf("%s\n\n", encoded_url_buffer); } else { FILE *curl_resp = tmpfile(); curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_resp); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, encoded_url_buffer); res = curl_easy_perform(curl); if(res != CURLE_OK) { fprintf(stderr, "cURL error %d\n", res); } } else { fprintf(stderr, "Cannot initiate cURL\n"); return 1; } curl_easy_cleanup(curl); curl_global_cleanup(); fclose(curl_resp); } } return 0; }
void gen_hot_subjects_xml(int mytype) { FILE *fp; char curfile[256]; char xml_buf[256]; char url_buf[256]; int i; if (mytype==4) { gen_blessing_list_xml(); return; } sprintf(curfile, "xml/%s.xml", myfile[mytype]); if ((fp = fopen(curfile, "w")) != NULL) { fprintf(fp, "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"); fprintf(fp, "<hotsubjects>\n"); for (i = 0; i < topnum; i++) { fprintf(fp, "<hotsubject>\n"); fprintf(fp, "<title>%s</title>\n", encode_url(url_buf,encode_xml(xml_buf, top[i].title, sizeof(xml_buf)),sizeof(url_buf))); fprintf(fp, "<author>%s</author>\n", encode_url(url_buf,top[i].userid,sizeof(url_buf))); fprintf(fp, "<board>%s</board>\n", encode_url(url_buf,top[i].board,sizeof(url_buf))); fprintf(fp, "<time>%ld</time>\n", top[i].date); fprintf(fp, "<number>%d</number>\n", top[i].number); fprintf(fp, "<groupid>%d</groupid>\n", top[i].groupid); fprintf(fp, "</hotsubject>\n"); } fprintf(fp, "</hotsubjects>\n"); fclose(fp); } #ifdef REDIS redisContext *redis = redisConnect("127.0.0.1", 6379); const char *pattern = "stat:%s:%s"; char key[128]; char value[512]; const char *argv[3] = { "RPUSH", key, value }; #define RPUSH(ikey,itype,ivalue) {\ snprintf(key, 128, pattern, myfile[mytype], ikey);\ snprintf(value, 512, itype, ivalue);\ redisAppendCommandArgv(redis, 3, argv, NULL);\ } #define DEL(ikey) {\ snprintf(key, 128, pattern, myfile[mytype], ikey);\ snprintf(value, 512, "DEL %s", key);\ redisAppendCommand(redis, value);\ } DEL("title"); DEL("author"); DEL("board"); DEL("replies"); DEL("time"); DEL("id"); for (i = 0; i < topnum; i++) { RPUSH("title" , "%s" , top[i].title); RPUSH("author" , "%s" , top[i].userid); RPUSH("board" , "%s" , top[i].board); RPUSH("replies" , "%lu" , (unsigned long)top[i].number); RPUSH("time" , "%lu" , (unsigned long)top[i].date); RPUSH("id" , "%lu" , (unsigned long)top[i].groupid); } redisReply *reply; redisGetReply(redis, (void*)&reply); freeReplyObject(reply); redisFree(redis); #undef RPUSH #undef DEL #endif }
int gen_commend_xml(const char *bname, const char *fn, int max) { int dirfd; FILE *fp; FILE *fp1; struct fileheader dirfh; char dirpath[STRLEN]; char dirfile[STRLEN]; char xml_buf[1024]; //changed from 256 to 1024, for <brief> - atppp char buf[256]; char url_buf[1024]; //changed from 256 to 1024, for <brief> - atppp struct stat st; int numrecords; int i, brieflen, len; char *c; struct boardheader *bh; setbfile(dirpath, bname, DIGEST_DIR); if (stat(dirpath, &st) < 0) return -1; numrecords = st.st_size / sizeof(struct fileheader) ; if (numrecords <= 0) return -1; if ((fp = fopen(fn, "w")) == NULL) return -1; fprintf(fp, "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"); fprintf(fp, "<hotsubjects>\n"); dirfd = open(dirpath, O_RDONLY); if (dirfd >= 0) { if (numrecords > max) lseek(dirfd, sizeof(struct fileheader)*(numrecords - max), SEEK_SET); numrecords -= max; while (read(dirfd, &dirfh, sizeof(dirfh)) >= sizeof(dirfh)) { setbfile(dirfile, bname, dirfh.filename); if ((fp1=fopen(dirfile, "r"))==NULL) continue; numrecords ++; fprintf(fp, "<hotsubject>\n"); fprintf(fp, "<title>%s</title>\n", encode_url(url_buf,encode_xml(xml_buf, dirfh.title, sizeof(xml_buf)),sizeof(url_buf))); fprintf(fp, "<author>%s</author>\n", encode_url(url_buf,dirfh.owner,sizeof(url_buf))); fprintf(fp, "<time>%ld</time>\n", get_posttime(&dirfh)); fprintf(fp, "<board>%s</board>\n", encode_url(url_buf,bname,sizeof(url_buf))); fprintf(fp, "<id>%d</id>\n", dirfh.id); fprintf(fp, "<groupid>%d</groupid>\n", dirfh.groupid); bh = (struct boardheader *) getboard(dirfh.o_bid); fprintf(fp, "<o_board>%s</o_board>\n", encode_url(url_buf,(bh ? bh->filename : ""),sizeof(url_buf))); fprintf(fp, "<o_id>%d</o_id>\n",dirfh.o_id); if (fgets(buf, 255, fp1)) { if (! strncmp(buf, "发信人: ", 8)) { if ((c=strchr(buf+8, ' ')) != NULL) *c = 0; fprintf(fp, "<owner>%s</owner>\n", buf+8); } } fprintf(fp, "<o_groupid>%d</o_groupid>\n<brief>", dirfh.o_groupid); for (i=0;i<3;i++) fgets(buf, 255, fp1); brieflen = 240; for (i=0;i<4;) { if (fgets(buf, 240, fp1)) { if (buf[0] == '\n' || buf[0] == '\r' || buf[0]=='\0') continue; buf[240]=0; /* etnlegend, 2006.09.17, 过滤控制字符... */ process_control_chars(buf,"\n"); len = strlen(buf); if (!(len<brieflen-1)) { strnzhcpy(buf,buf,brieflen); len=brieflen; } fprintf(fp, "%s", encode_url(url_buf,encode_xml(xml_buf, buf, sizeof(xml_buf)),sizeof(url_buf))); i++; brieflen -= len; if (brieflen<2) break; } else break; } fprintf(fp, " </brief>\n</hotsubject>\n"); fclose(fp1); } close(dirfd); } fprintf(fp, "</hotsubjects>\n"); fclose(fp); return 0; }