**First, the detailed explanation of SNMP programming**
SNMP (Simple Network Management Protocol) is a remote monitoring application that operates over TCP/UDP. It is used to monitor various aspects of devices on a network, such as memory usage, CPU utilization, and disk space. The protocol is typically divided into two main components: the SNMP server, which runs on the device being monitored, and the SNMP client, which resides on the monitoring system.
The SNMP server collects data from the host and organizes it in a hierarchical structure known as a Management Information Base (MIB). This structure resembles a tree, where each node represents a specific piece of information. For example, the root of the tree is empty, and each child node is assigned a unique number. These numbers are connected using dots to form an OID (Object Identifier), which acts as a unique address for each piece of data.
On the other hand, the SNMP client sends queries to the server to retrieve real-time information. There are two main types of requests: GET, which retrieves specific information, and GETNEXT, which fetches the next item in the MIB hierarchy. Additionally, the SNMP server can send TRAP messages to the client when an abnormal event occurs, allowing the monitoring system to respond immediately.
**Second, SNMP installation**
Most Linux distributions come with SNMP pre-installed. You can check its presence by running `whereis snmpd`. If it's not installed, you can download and install net-snmp either via source or package manager.
For source installation:
1. Run `./configure`, then `make`, followed by `make install`.
For package managers like yum (on CentOS):
- `yum install net-snmp` for the core service.
- `yum install net-snmp-devel` for development support.
- `yum install net-snmp-utils` for testing tools.
**Third, SNMP configuration**
After installation, configure the SNMP server by copying the example configuration file (`snmpd.conf`) to a desired location, such as `/usr/local/etc/snmp/`. Modify the configuration to allow access:
- Change the `agentAddress` from `udp:127.0.0.1:161` to the external IP address of the machine so it can be accessed remotely.
- Adjust the view settings to include all necessary OIDs, such as `.1` for full access.
**Fourth, testing and troubleshooting**
Once configured, start the SNMP service using `snmpd` or the appropriate path if installed from source. Test it with commands like `snmpwalk -v 1 localhost -c public .1`. To test remote access, run the same command from the monitoring host, replacing `localhost` with the target IP.
If remote access fails, disable the firewall (`service iptables stop`) and set SELinux to permissive mode (`setenforce 0`). These steps help ensure no network or security policies block the communication.
**Fifth, SNMP C programming**
The NET-SNMP library provides a powerful API for integrating custom scripts or programs into the SNMP framework. By adding lines to `snmpd.conf`, you can execute external programs when specific OIDs are queried. For example:
`Extend .1.3.6.1.4.1.2021.50 monitor /bin/sh /tmp/monitor.sh`
This allows the script `monitor.sh` to run whenever the specified OID is accessed.
On the client side, C programs can use the NET-SNMP library to query SNMP data. Here’s a basic example:
```c
#include "net-snmp/net-snmp-config.h"
#include "net-snmp/net-snmp-includes.h"
#include
int find_last_oid(netsnmp_session *ss, oid *base, int base_length) {
netsnmp_pdu *response;
netsnmp_pdu *pdu;
int running = 1;
int status;
int length = 0;
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, base, base_length);
while (running) {
status = snmp_synch_response(ss, pdu, &response);
if (status != STAT_SUCCESS || !response) {
snmp_sess_perror("snmp_synch_response", ss);
exit(1);
}
if (response->errstat != SNMP_ERR_NOERROR) {
fprintf(stderr, "snmp: Error in packet: %s\n", snmp_errstring(response->errstat));
exit(1);
}
if (snmp_oid_compare(response->variables->name, SNMP_MIN(base_length, response->variables->name_length), base, base_length) != 0)
running = 0;
else {
memcpy(base, response->variables->name, response->variables->name_length * sizeof(oid));
length = response->variables->name_length;
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, response->variables->name, response->variables->name_length);
}
snmp_free_pdu(response);
}
return length;
}
int main() {
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
struct variable_list *vars;
oid base[128] = {1, 3, 6, 1, 4, 1, 2021, 50};
size_t base_length = 8;
int status;
init_snmp("APC Check");
snmp_sess_init(&session);
session.version = SNMP_VERSION_1;
session.community = (u_char*) "public";
session.community_len = strlen((const char*)session.community);
session.peername = "10.0.1.3"; // IP of the monitored host
ss = snmp_open(&session);
if (!ss) {
snmp_sess_perror("snmp_open", &session);
exit(1);
}
int new_length = find_last_oid(ss, base, base_length);
pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, base, new_length);
status = snmp_synch_response(ss, pdu, &response);
if (status != STAT_SUCCESS || !response) {
snmp_sess_perror("snmp_synch_response", ss);
exit(1);
}
for (vars = response->variables; vars; vars = vars->next_variable) {
for (int i = 0; i < vars->name_length; i++) {
printf("%d.", vars->name[i]);
}
print_value(vars->name, vars->name_length, vars);
}
snmp_free_pdu(response);
snmp_close(ss);
return 0;
}
```
This program demonstrates how to query SNMP data using the NET-SNMP library in C, making it easier to integrate custom monitoring logic into your network management systems.
Paste Type Rotor Production Line
Paste Type Rotor Production Line,Dc Refrigeration Compressor Rotor Production,Rotor Production Line,Automatic Adhesive Rotor Production Line
Suzhou Mountain Industrial Control Equipment Co., Ltd , https://www.szmountain.com