📄 mta_sendmail.c
字号:
* * nothing * */void init_mta_sendmail(void){ REGISTER_MIB("mibII/mta_sendmail", mta_variables, variable4, mta_variables_oid); snmpd_register_config_handler("sendmail_config" , mta_sendmail_parse_config, NULL, "file"); snmpd_register_config_handler("sendmail_stats" , mta_sendmail_parse_config, NULL, "file"); snmpd_register_config_handler("sendmail_queue" , mta_sendmail_parse_config, NULL, "directory"); snmpd_register_config_handler("sendmail_index" , mta_sendmail_parse_config, NULL, "integer"); snmpd_register_config_handler("sendmail_stats_t", mta_sendmail_parse_config, NULL, "cachetime/sec"); snmpd_register_config_handler("sendmail_queue_t", mta_sendmail_parse_config, NULL, "cachetime/sec"); strcpy(sendmailcf_fn, "/etc/mail/sendmail.cf"); if (read_sendmailcf(FALSE) == FALSE) { strcpy(sendmailcf_fn, "/etc/sendmail.cf"); read_sendmailcf(FALSE); } if (sendmailst_fh == -1) { strcpy(sendmailst_fn, "/etc/mail/statistics"); open_sendmailst(FALSE); if (sendmailst_fh == -1) { strcpy(sendmailst_fn, "/etc/mail/sendmail.st"); open_sendmailst(FALSE); } } if (mqueue_dp == NULL) { strcpy(mqueue_dn, "/var/spool/mqueue"); mqueue_dp = opendir(mqueue_dn); }}/**//** unsigned char *var_mtaEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method) * * Description: * * Called by the agent in order to get the values for the mtaTable. * * Parameters: * * see agent documentation * * Returns: * * see agent documentation * */unsigned char *var_mtaEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static long long_ret; auto int i; auto int result; auto time_t current_time; if (exact) { if (*length != vp->namelen + 1) { return NULL; } result = snmp_oid_compare(name, *length - 1, vp->name, vp->namelen); if (result != 0 || name[*length - 1] != applindex) { return NULL; } } else { if (*length <= vp->namelen) { result = -1; } else { result = snmp_oid_compare(name, *length - 1, vp->name, vp->namelen); } if (result > 0) { return NULL; } if (result == 0 && name[*length - 1] >= applindex) { return NULL; } if (result < 0) { memcpy(name, vp->name, (int) vp->namelen * (int) sizeof *name); *length = vp->namelen + 1; } name[vp->namelen] = applindex; } *write_method = (WriteMethod *) NULL; *var_len = sizeof(long); /* default to 'long' results */ if (vp->magic & NEEDS_STATS) { if (sendmailst_fh == -1) return NULL; current_time = time(NULL); if (current_time == (time_t) -1 || current_time > lastreadstats + stat_cache_time) { if (lseek(sendmailst_fh, 0, SEEK_SET) == -1) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not rewind to the beginning of file \"%s\"\n", sendmailst_fn); return NULL; } if (read(sendmailst_fh, (void *)&stats, stats_size) != stats_size) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not read from statistics file \"%s\"\n", sendmailst_fn); return NULL; } if (current_time != (time_t) -1) { lastreadstats = current_time; } } } if (vp->magic & NEEDS_DIR) { if (mqueue_dp == NULL) return NULL; current_time = time(NULL); if (current_time == (time_t) -1 || current_time > lastreaddir + dir_cache_time) { struct dirent *dirptr; struct stat filestat; char cwd[200]; if(getcwd(cwd, sizeof cwd) == NULL) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not get current working directory\n"); return NULL; } if(chdir(mqueue_dn) != 0) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not enter mailqueue directory \"%s\"\n", mqueue_dn); return NULL; } rewinddir(mqueue_dp); mqueue_count = 0; mqueue_size = 0; while ((dirptr = readdir(mqueue_dp)) != NULL) { if(dirptr->d_name[0] == 'd' && dirptr->d_name[1] == 'f') { if(stat(dirptr->d_name, &filestat) == 0) { mqueue_size += (filestat.st_size + 999) / 1000; /* That's how sendmail calculates it's statistics too */ mqueue_count++; } else { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not get size of file \"%s\" in directory \"%s\"\n", dirptr->d_name, mqueue_dn); if (chdir(cwd) != 0) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not go back to directory \"%s\" where I just came from\n", mqueue_dn); } return NULL; } } } if (chdir(cwd) != 0) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaEntry: could not go back to directory \"%s\" where I just came from\n", mqueue_dn); } if (current_time != (time_t) -1) { lastreaddir = current_time; } } } switch(vp->magic) { case MTARECEIVEDMESSAGES: long_ret = 0; for ( i=0; i<MAXMAILERS; i++) { long_ret += stat_nf[i]; } return (unsigned char *) &long_ret; case MTASTOREDMESSAGES: long_ret = mqueue_count; return (unsigned char *) &long_ret; case MTATRANSMITTEDMESSAGES: long_ret = 0; for ( i=0; i<MAXMAILERS; i++) { long_ret += stat_nt[i]; } return (unsigned char *) &long_ret; case MTARECEIVEDVOLUME: long_ret = 0; for ( i=0; i<MAXMAILERS; i++) { long_ret += stat_bf[i]; } return (unsigned char *) &long_ret; case MTASTOREDVOLUME: long_ret = mqueue_size; return (unsigned char *) &long_ret; case MTATRANSMITTEDVOLUME: long_ret = 0; for ( i=0; i<MAXMAILERS; i++) { long_ret += stat_bt[i]; } return (unsigned char *) &long_ret; default: snmp_log(LOG_ERR, "mibII/mta_sendmail.c:mtaEntry: unknown magic value\n"); } return NULL;}/**//** unsigned char *var_mtaGroupEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method) * * Description: * * Called by the agent in order to get the values for the mtaGroupTable. * * Parameters: * * see agent documentation * * Returns: * * see agent documentation * */unsigned char *var_mtaGroupEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static long long_ret; auto long row; auto int result; auto time_t current_time; if (exact) { if (*length != vp->namelen + 2) { return NULL; } result = snmp_oid_compare(name, *length - 2, vp->name, vp->namelen); if (result != 0 || name[*length - 2] != applindex || name[*length - 1] <= 0 || name[*length - 1] > mailers) { return NULL; } } else { if (*length < vp->namelen) { result = -1; } else { result = snmp_oid_compare(name, vp->namelen, vp->name, vp->namelen); } if (result > 0) /* OID prefix too large */ { return NULL; } if (result == 0) /* OID prefix matches exactly,... */ { if (*length > vp->namelen && name[vp->namelen] > applindex) /* ... but ApplIndex too large */ { return NULL; } if (*length > vp->namelen && name[vp->namelen] == applindex) /* ... ApplIndex ok,... */ { if (*length > vp->namelen + 1 && name[vp->namelen + 1] >= 1) { if (name[vp->namelen + 1] >= mailers) /* ... but mailernr too large */ { return NULL; } else { name[vp->namelen + 1] ++ ; } } else { name[vp->namelen + 1] = 1; } } else { name[vp->namelen] = applindex; name[vp->namelen + 1] = 1; } } else { /* OID prefix too small */ memcpy(name, vp->name, (int) vp->namelen * (int) sizeof *name); name[vp->namelen] = applindex; name[vp->namelen + 1] = 1; } *length = vp->namelen + 2; } *write_method = 0; *var_len = sizeof(long); /* default to 'long' results */ if (vp->magic & NEEDS_STATS) { if (sendmailst_fh == -1) return NULL; current_time = time(NULL); if (current_time == (time_t) -1 || current_time > lastreadstats + stat_cache_time) { if (lseek(sendmailst_fh, 0, SEEK_SET) == -1) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaGroupEntry: could not rewind to beginning of file \"%s\"\n", sendmailst_fn); return NULL; } if (read(sendmailst_fh, (void *)&stats, stats_size) != stats_size) { snmp_log(LOG_ERR, "mibII/mta_sendmail.c:var_mtaGroupEntry: could not read statistics file \"%s\"\n", sendmailst_fn); return NULL; } if (current_time != (time_t) -1) { lastreadstats = current_time; } } } row = name[*length - 1] - 1; switch(vp->magic) { case MTAGROUPRECEIVEDMESSAGES: long_ret = (long) stat_nf[row]; return (unsigned char *) &long_ret; case MTAGROUPREJECTEDMESSAGES: if (stat_nr != NULL && stat_nd != NULL) { long_ret = (long) (stat_nr[row] + stat_nd[row]); /* Number of rejected plus number of discarded messages */ return (unsigned char *) &long_ret; } else { return NULL; } case MTAGROUPTRANSMITTEDMESSAGES: long_ret = (long) stat_nt[row]; return (unsigned char *) &long_ret; case MTAGROUPRECEIVEDVOLUME: long_ret = (long) stat_bf[row]; return (unsigned char *) &long_ret; case MTAGROUPTRANSMITTEDVOLUME: long_ret = (long) stat_bt[row]; return (unsigned char *) &long_ret; case MTAGROUPNAME: *var_len=strlen(mailernames[row]); return (unsigned char *) (*var_len > 0 ? mailernames[row] : NULL); case MTAGROUPHIERARCHY: long_ret = (long) -1; return (unsigned char *) &long_ret; default: snmp_log(LOG_ERR, "mibII/mta_sendmail.c:mtaGroupEntry: unknown magic value\n"); } return NULL;}/**/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -