View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.protocol.snmp;
21  
22  import org.snmp4j.agent.DuplicateRegistrationException;
23  import org.snmp4j.agent.MOScope;
24  import org.snmp4j.mp.SnmpConstants;
25  import org.snmp4j.smi.Gauge32;
26  import org.snmp4j.smi.OctetString;
27  import org.snmp4j.smi.TimeTicks;
28  import org.snmp4j.smi.VariableBinding;
29  import org.waarp.common.logging.WaarpLogger;
30  import org.waarp.common.logging.WaarpLoggerFactory;
31  import org.waarp.openr66.database.data.DbTaskRunner;
32  import org.waarp.openr66.protocol.configuration.Configuration;
33  import org.waarp.openr66.protocol.localhandler.packet.RequestPacket.TRANSFERMODE;
34  import org.waarp.openr66.protocol.utils.Version;
35  import org.waarp.snmp.r66.WaarpPrivateMib;
36  import org.waarp.snmp.utils.MemoryGauge32;
37  import org.waarp.snmp.utils.MemoryGauge32.MemoryType;
38  import org.waarp.snmp.utils.WaarpMORow;
39  import org.waarp.snmp.utils.WaarpMOScalar;
40  import org.waarp.snmp.utils.WaarpUptime;
41  
42  /**
43   * Waarp OpenR66 Private MIB implementation
44   */
45  public class R66PrivateMib extends WaarpPrivateMib {
46    /**
47     * Internal Logger
48     */
49    private static final WaarpLogger logger =
50        WaarpLoggerFactory.getLogger(R66PrivateMib.class);
51  
52    /**
53     * @param sysdesc
54     * @param port
55     * @param smiPrivateCodeFinal
56     * @param typeWaarpObject
57     * @param scontactName
58     * @param stextualName
59     * @param saddress
60     * @param iservice
61     */
62    public R66PrivateMib(final String sysdesc, final int port,
63                         final int smiPrivateCodeFinal, final int typeWaarpObject,
64                         final String scontactName, final String stextualName,
65                         final String saddress, final int iservice) {
66      super(sysdesc, port, smiPrivateCodeFinal, typeWaarpObject, scontactName,
67            stextualName, saddress, iservice);
68    }
69  
70    @Override
71    protected final void agentRegisterWaarpMib()
72        throws DuplicateRegistrationException {
73      logger.debug("registerGGMib");
74      // register Static info
75      rowInfo = new WaarpMORow(this, rootOIDWaarpInfo, WaarpDefinition,
76                               MibLevel.staticInfo.ordinal());
77      rowInfo.setValue(WaarpDefinitionIndex.applName.ordinal(), "Waarp OpenR66");
78      rowInfo.setValue(WaarpDefinitionIndex.applServerName.ordinal(),
79                       Configuration.configuration.getHostId());
80      rowInfo.setValue(WaarpDefinitionIndex.applVersion.ordinal(), Version.ID);
81      rowInfo.setValue(WaarpDefinitionIndex.applDescription.ordinal(),
82                       "Waarp OpenR66: File Transfer Monitor");
83      rowInfo.setValue(WaarpDefinitionIndex.applURL.ordinal(),
84                       "http://waarp.github.com/Waarp");
85      rowInfo.setValue(WaarpDefinitionIndex.applApplicationProtocol.ordinal(),
86                       applicationProtocol);
87  
88      rowInfo.registerMOs(agent.getServer(), null);
89      // register General info
90      rowGlobal = new WaarpMORow(this, rootOIDWaarpGlobal, WaarpGlobalValues,
91                                 MibLevel.globalInfo.ordinal());
92      WaarpMOScalar memoryScalar =
93          rowGlobal.getRow()[WaarpGlobalValuesIndex.memoryTotal.ordinal()];
94      memoryScalar.setValue(new MemoryGauge32(MemoryType.TotalMemory));
95      memoryScalar =
96          rowGlobal.getRow()[WaarpGlobalValuesIndex.memoryFree.ordinal()];
97      memoryScalar.setValue(new MemoryGauge32(MemoryType.FreeMemory));
98      memoryScalar =
99          rowGlobal.getRow()[WaarpGlobalValuesIndex.memoryUsed.ordinal()];
100     memoryScalar.setValue(new MemoryGauge32(MemoryType.UsedMemory));
101     rowGlobal.registerMOs(agent.getServer(), null);
102     // setup UpTime to SysUpTime and change status
103     scalarUptime =
104         rowGlobal.getRow()[WaarpGlobalValuesIndex.applUptime.ordinal()];
105     scalarUptime.setValue(new WaarpUptime(upTime));
106     changeStatus(OperStatus.restarting);
107     changeStatus(OperStatus.up);
108     // register Detailed info
109     rowDetailed =
110         new WaarpMORow(this, rootOIDWaarpDetailed, WaarpDetailedValues,
111                        MibLevel.detailedInfo.ordinal());
112     rowDetailed.registerMOs(agent.getServer(), null);
113     // register Error info
114     rowError = new WaarpMORow(this, rootOIDWaarpError, WaarpErrorValues,
115                               MibLevel.errorInfo.ordinal());
116     rowError.registerMOs(agent.getServer(), null);
117   }
118 
119   /**
120    * Send a notification (trap or inform) for Shutdown event
121    *
122    * @param message
123    * @param message2
124    */
125   public final void notifyStartStop(final String message,
126                                     final String message2) {
127     if (!TrapLevel.StartStop.isLevelValid(agent.getTrapLevel())) {
128       return;
129     }
130     notify(NotificationElements.TrapShutdown, message, message2);
131   }
132 
133   /**
134    * Send a notification (trap or inform) for Error event
135    *
136    * @param message
137    * @param message2
138    */
139   public final void notifyError(final String message, final String message2) {
140     if (!TrapLevel.Alert.isLevelValid(agent.getTrapLevel())) {
141       return;
142     }
143     notify(NotificationElements.TrapError, message, message2);
144   }
145 
146   /**
147    * Send a notification (trap or inform) for Server Overloaded event
148    *
149    * @param message
150    * @param message2
151    */
152   public final void notifyOverloaded(final String message,
153                                      final String message2) {
154     if (!TrapLevel.Warning.isLevelValid(agent.getTrapLevel())) {
155       return;
156     }
157     notify(NotificationElements.TrapOverloaded, message, message2);
158   }
159 
160   /**
161    * Send a notification (trap or inform) for Warning event
162    *
163    * @param message
164    * @param message2
165    */
166   public final void notifyWarning(final String message, final String message2) {
167     if (!TrapLevel.Warning.isLevelValid(agent.getTrapLevel())) {
168       return;
169     }
170     notify(NotificationElements.TrapWarning, message, message2);
171   }
172 
173   /**
174    * Send a notification (trap or inform)
175    *
176    * @param message
177    * @param runner
178    */
179   public final void notifyInternalTask(final String message,
180                                        final DbTaskRunner runner) {
181     try {
182       long delay =
183           (runner.getStart().getTime() - agent.getUptimeSystemTime()) / 10;
184       if (delay < 0) {
185         delay = 0;
186       }
187       agent.getNotificationOriginator().notify(new OctetString("public"),
188                                                NotificationElements.InfoTask.getOID(
189                                                    rootOIDWaarpNotif),
190                                                new VariableBinding[] {
191                                                    new VariableBinding(
192                                                        NotificationElements.InfoTask.getOID(
193                                                            rootOIDWaarpNotif,
194                                                            1), new OctetString(
195                                                        NotificationElements.InfoTask.name())),
196                                                    new VariableBinding(
197                                                        NotificationElements.InfoTask.getOID(
198                                                            rootOIDWaarpNotif,
199                                                            1), new OctetString(
200                                                        message)),
201                                                    // Start of Task
202                                                    new VariableBinding(
203                                                        NotificationElements.InfoTask.getOID(
204                                                            rootOIDWaarpNotif,
205                                                            NotificationTasks.globalStepInfo.getOID()),
206                                                        new Gauge32(
207                                                            runner.getGloballaststep())),
208                                                    new VariableBinding(
209                                                        NotificationElements.InfoTask.getOID(
210                                                            rootOIDWaarpNotif,
211                                                            NotificationTasks.stepInfo.getOID()),
212                                                        new Gauge32(
213                                                            runner.getStep() +
214                                                            1)),
215                                                    new VariableBinding(
216                                                        NotificationElements.InfoTask.getOID(
217                                                            rootOIDWaarpNotif,
218                                                            NotificationTasks.rankFileInfo.getOID()),
219                                                        new Gauge32(
220                                                            runner.getRank())),
221                                                    new VariableBinding(
222                                                        NotificationElements.InfoTask.getOID(
223                                                            rootOIDWaarpNotif,
224                                                            NotificationTasks.stepStatusInfo.getOID()),
225                                                        new OctetString(
226                                                            runner.getStatus()
227                                                                  .getMesg())),
228                                                    new VariableBinding(
229                                                        NotificationElements.InfoTask.getOID(
230                                                            rootOIDWaarpNotif,
231                                                            NotificationTasks.filenameInfo.getOID()),
232                                                        new OctetString(
233                                                            runner.getFilename())),
234                                                    new VariableBinding(
235                                                        NotificationElements.InfoTask.getOID(
236                                                            rootOIDWaarpNotif,
237                                                            NotificationTasks.originalNameInfo.getOID()),
238                                                        new OctetString(
239                                                            runner.getOriginalFilename())),
240                                                    new VariableBinding(
241                                                        NotificationElements.InfoTask.getOID(
242                                                            rootOIDWaarpNotif,
243                                                            NotificationTasks.idRuleInfo.getOID()),
244                                                        new OctetString(
245                                                            runner.getRuleId())),
246                                                    new VariableBinding(
247                                                        NotificationElements.InfoTask.getOID(
248                                                            rootOIDWaarpNotif,
249                                                            NotificationTasks.modeTransInfo.getOID()),
250                                                        new OctetString(
251                                                            TRANSFERMODE.values()[runner.getMode()].name())),
252                                                    new VariableBinding(
253                                                        NotificationElements.InfoTask.getOID(
254                                                            rootOIDWaarpNotif,
255                                                            NotificationTasks.retrieveModeInfo.getOID()),
256                                                        new OctetString(
257                                                            runner.isSender()?
258                                                                "Sender" :
259                                                                "Receiver")),
260                                                    new VariableBinding(
261                                                        NotificationElements.InfoTask.getOID(
262                                                            rootOIDWaarpNotif,
263                                                            NotificationTasks.startTransInfo.getOID()),
264                                                        new TimeTicks(delay)),
265                                                    new VariableBinding(
266                                                        NotificationElements.InfoTask.getOID(
267                                                            rootOIDWaarpNotif,
268                                                            NotificationTasks.infoStatusInfo.getOID()),
269                                                        new OctetString(
270                                                            runner.getErrorInfo()
271                                                                  .getMesg())),
272                                                    new VariableBinding(
273                                                        NotificationElements.InfoTask.getOID(
274                                                            rootOIDWaarpNotif,
275                                                            NotificationTasks.requesterInfo.getOID()),
276                                                        new OctetString(
277                                                            runner.getRequester())),
278                                                    new VariableBinding(
279                                                        NotificationElements.InfoTask.getOID(
280                                                            rootOIDWaarpNotif,
281                                                            NotificationTasks.requestedInfo.getOID()),
282                                                        new OctetString(
283                                                            runner.getRequested())),
284                                                    new VariableBinding(
285                                                        NotificationElements.InfoTask.getOID(
286                                                            rootOIDWaarpNotif,
287                                                            NotificationTasks.specialIdInfo.getOID()),
288                                                        new OctetString(
289                                                            String.valueOf(
290                                                                runner.getSpecialId()))),
291                                                    // End of Task
292                                                    new VariableBinding(
293                                                        SnmpConstants.sysDescr,
294                                                        snmpv2.getDescr()),
295                                                    new VariableBinding(
296                                                        SnmpConstants.sysObjectID,
297                                                        snmpv2.getObjectID()),
298                                                    new VariableBinding(
299                                                        SnmpConstants.sysContact,
300                                                        snmpv2.getContact()),
301                                                    new VariableBinding(
302                                                        SnmpConstants.sysName,
303                                                        snmpv2.getName()),
304                                                    new VariableBinding(
305                                                        SnmpConstants.sysLocation,
306                                                        snmpv2.getLocation())
307                                                });
308     } catch (final NullPointerException ignored) {
309       // nothing
310     }
311   }
312 
313   /**
314    * Send a notification (trap or inform) for Warning/Error event on a single
315    * Transfer Task
316    *
317    * @param message
318    * @param runner
319    */
320   public final void notifyInfoTask(final String message,
321                                    final DbTaskRunner runner) {
322     if (!TrapLevel.All.isLevelValid(agent.getTrapLevel())) {
323       return;
324     }
325     if (logger.isDebugEnabled()) {
326       logger.debug("Notify: {}:{}:{}", NotificationElements.InfoTask, message,
327                    runner.toShortString());
328     }
329     notifyInternalTask(message, runner);
330   }
331 
332   /**
333    * Send a notification (trap or inform) for all events on a single Transfer
334    * Task
335    *
336    * @param message
337    * @param runner
338    */
339   public final void notifyTask(final String message,
340                                final DbTaskRunner runner) {
341     if (!TrapLevel.AllEvents.isLevelValid(agent.getTrapLevel())) {
342       return;
343     }
344     if (logger.isDebugEnabled()) {
345       logger.debug("Notify: {}:{}:{}", NotificationElements.InfoTask, message,
346                    runner.toShortString());
347     }
348     notifyInternalTask(message, runner);
349   }
350 
351   /**
352    * Trap/Notification
353    *
354    * @param element
355    * @param message
356    * @param message2
357    */
358   private final void notify(final NotificationElements element,
359                             final String message, final String message2) {
360     try {
361       if (logger.isDebugEnabled()) {
362         logger.debug("Notify: {}:{}:{}", element, message, message2);
363       }
364       agent.getNotificationOriginator()
365            .notify(new OctetString("public"), element.getOID(rootOIDWaarpNotif),
366                    new VariableBinding[] {
367                        new VariableBinding(element.getOID(rootOIDWaarpNotif, 1),
368                                            new OctetString(element.name())),
369                        new VariableBinding(element.getOID(rootOIDWaarpNotif, 1),
370                                            new OctetString(message)),
371                        new VariableBinding(element.getOID(rootOIDWaarpNotif, 1),
372                                            new OctetString(message2)),
373                        new VariableBinding(SnmpConstants.sysDescr,
374                                            snmpv2.getDescr()),
375                        new VariableBinding(SnmpConstants.sysObjectID,
376                                            snmpv2.getObjectID()),
377                        new VariableBinding(SnmpConstants.sysContact,
378                                            snmpv2.getContact()),
379                        new VariableBinding(SnmpConstants.sysName,
380                                            snmpv2.getName()),
381                        new VariableBinding(SnmpConstants.sysLocation,
382                                            snmpv2.getLocation())
383                    });
384     } catch (final NullPointerException ignored) {
385       // nothing
386     }
387   }
388 
389   @Override
390   public final void updateServices(final WaarpMOScalar scalar) {
391     // nothing
392   }
393 
394   @Override
395   public final void updateServices(final MOScope range) {
396     // nothing
397   }
398 
399 }