首页 \ 问答 \ 如何使用Net-SNMP API编写发送用户定义的陷阱的代码(How to write a code sending user defined traps by using Net-SNMP API)

如何使用Net-SNMP API编写发送用户定义的陷阱的代码(How to write a code sending user defined traps by using Net-SNMP API)

我们是否可以编写用于发送用户定义的陷阱的代码而不是使用Net-SNMP API(如下所示)在snmpd中提到的代码来发送陷阱netsnmp_send_traps(),send_v2trap(),send_v3trap()?

我不打算编写任何MIB模块。 所以我不希望我的陷阱应该通过snmpd(代理守护进程)。

我想实现的想法是从外部代码向snmptrapd发送陷阱,snmptrapd将接收陷阱并将其转发到配置的陷阱接收器,以便更熟悉Net-SNMP库。


Can we write the code for sending user defined traps rather than those mentioned in snmpd using Net-SNMP API like below to send traps netsnmp_send_traps(), send_v2trap(), send_v3trap()?

I am not planning to write any MIB module. So I don't expect that my trap should be going through snmpd (Agent Daemon).

The idea I want to implement is that sending trap from external code to snmptrapd which will receive traps and forward it to configured trap receiver to get more familiar with Net-SNMP library.


原文:https://stackoverflow.com/questions/20345444
更新时间:2022-03-28 11:03

最满意答案

所以现在我已经尝试使用Scalaz Either(这是一个正确的偏向Either与中性scala Either相比)和Monad Transformer EitherT,它似乎完全符合我的要求。 感谢Huw,特别是Lars Hupel暗示我正确的方向。

这里有一个Twitter期货和Scalaz Either和EitherT的样本:

import com.twitter.util.{Await, Future}
import scalaz.{Monad, Functor, EitherT, \/}
import scalaz.syntax.ToIdOps

object EitherTest extends App with ToIdOps{

  // make Twitter futures work with EitherT
  implicit val FutureFunctor = new Functor[Future] {
    def map[A, B](a: Future[A])(f: A => B): Future[B] = a map f
  }
  implicit val FutureMonad = new Monad[Future] {
    def point[A](a: => A): Future[A] = Future(a)
    def bind[A, B](fa: Future[A])(f: (A) => Future[B]): Future[B] = fa flatMap f
  }

  // The example begins here:

  case class InvalidInfo(error: String)
  case class Response(msg: String)


  class ComponentA {
    def foo(fail: Boolean): Future[\/[InvalidInfo, Response]] = {
      if(fail) Future(InvalidInfo("Error A").left) else Future(Response("ComponentA Success").right)
    }
  }
  class ComponentB {
    def bar(fail: Boolean): Future[\/[InvalidInfo, Response]] = {
      if(fail) Future(InvalidInfo("Error B").left) else Future(Response("ComponentB Success").right)
    }
  }

  val a = new ComponentA
  val b = new ComponentB

  val result = for {
    resultA <- EitherT(a.foo(false))
    resultB <- EitherT(b.bar(false))
  } yield (resultA, resultB)

  println(Await.result(result.run))
}

So now I've tried to use Scalaz Either (which is a right biased Either compared to the neutral scala Either) and the Monad Transformer EitherT and it seems it does exactly what I want. Thanks to Huw and especially Lars Hupel for hinting me in the right direction.

Here is working a sample for Twitter futures and Scalaz Either and EitherT:

import com.twitter.util.{Await, Future}
import scalaz.{Monad, Functor, EitherT, \/}
import scalaz.syntax.ToIdOps

object EitherTest extends App with ToIdOps{

  // make Twitter futures work with EitherT
  implicit val FutureFunctor = new Functor[Future] {
    def map[A, B](a: Future[A])(f: A => B): Future[B] = a map f
  }
  implicit val FutureMonad = new Monad[Future] {
    def point[A](a: => A): Future[A] = Future(a)
    def bind[A, B](fa: Future[A])(f: (A) => Future[B]): Future[B] = fa flatMap f
  }

  // The example begins here:

  case class InvalidInfo(error: String)
  case class Response(msg: String)


  class ComponentA {
    def foo(fail: Boolean): Future[\/[InvalidInfo, Response]] = {
      if(fail) Future(InvalidInfo("Error A").left) else Future(Response("ComponentA Success").right)
    }
  }
  class ComponentB {
    def bar(fail: Boolean): Future[\/[InvalidInfo, Response]] = {
      if(fail) Future(InvalidInfo("Error B").left) else Future(Response("ComponentB Success").right)
    }
  }

  val a = new ComponentA
  val b = new ComponentB

  val result = for {
    resultA <- EitherT(a.foo(false))
    resultB <- EitherT(b.bar(false))
  } yield (resultA, resultB)

  println(Await.result(result.run))
}

相关问答

更多
  • 您可以将期货和承诺视为管道的两个不同方面。 在承诺方面,数据被推入,而未来方面,数据可以被抽出。 未来是某种异步操作,可以在不同的执行路径中完成。 实际上,未来是一个占位符对象 ,可以在某个时间点异步地获得某个值。 它不是异步计算本身。 事实上,有一个名为future的未来构造函数返回这样的占位符对象, 并生成完成此占位符对象的异步计算并不意味着异步计算被称为未来 。 还有其他未来的建设者/工厂方法 。 但我没有得到的是,诺言有未来吗? 将承诺和期货划分为2个独立的界面是设计决策。 你可以在Future的同 ...
  • 我不确定我是否完全理解你的问题,但我至少可以在第一期提供答案。 在onComplete访问可变状态可能是不安全的。 实际上它会使演员的整个目的无效。 如果您访问可变状态,您应该始终通过actor的邮箱进行操作,例如self ! ModifyState(newValue) self ! ModifyState(newValue) 。 这里的问题是onComplete方法与actor不在同一个线程中,导致可能两个线程同时修改相同数据的情况,因此变异状态具有通常在并发状态变化中存在的所有相同问题(种族)条件等)。 ...
  • 期货开始在这里执行: val fFuture: Future[Int] = Future { println("f called"); 3 } val gFuture: Future[Int] = Future { println("g called"); 4 } 因此两个执 ...
  • recover会将Future恢复为默认值。 在这种情况下, String 。 你想要的是恢复与另一个 Future恢复Future 。 def zip(key: String): Future[String] = { val zipFileName = key + ".zip" val zipFile = bucket get zipFileName //Returns a Future[BucketFile] or S3Exception zipFile.map(bucketFi ...
  • 当你处理可变状态时,Actor很好,因为它们封装了可变状态。 并且只允许基于消息的交互。 您可以使用Future在不同的线程中执行。 因为Scala的Future所以你不必阻止Future 。 因此,如果您的代码中有多个期货,则无需等待/阻止所有这些期货进行竞争。 例如,如果您的管道完全是非阻塞或asyn(例如,Play和Spray),您可以将Future返回给客户端。 与演员相比,期货是轻量级的,因为你不需要一个完整的演员系统。 以下是Martin Odersky的一句话,我非常喜欢。 所有并发问题都没有 ...
  • 这实际上似乎对我很好。 它显示 [DEBUG 08:06:45] futures: List(Future(), Future(), Future(), Future(), Future()) [DEBUG 08:06:45] result: List(2, 4, 6, 8, 10) 使用自定义记录器在Scalatest内运行 System.out.prin ...
  • 简答:没有。 答案很长: object MultiThreadCalc { def run(matrix : Seq[Seq[Double]]) : Future[Seq[Seq[Double]] = Future{doMath(matrix)}(ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))) } 对于每次run调用,上面的代码将分配一个永远不会关闭的新线程池,它将用于执行单个方法doMath (因此将只使用该池 ...
  • 所以现在我已经尝试使用Scalaz Either(这是一个正确的偏向Either与中性scala Either相比)和Monad Transformer EitherT,它似乎完全符合我的要求。 感谢Huw,特别是Lars Hupel暗示我正确的方向。 这里有一个Twitter期货和Scalaz Either和EitherT的样本: import com.twitter.util.{Await, Future} import scalaz.{Monad, Functor, EitherT, \/} impo ...
  • 你必须使用flatMap ,因为如果第一个操作没有返回结果你想要做的事情也会返回一个未来。 这仍然是在编译时尽可能接近您的代码。 请注意,您不能在scala中使用名为val标识符,因为这是一个关键字。 def getValue(v: Foo)(implicit ec: ExecutionContext): Future[Value] = { val resultFuture: Future[Seq[Value2]] = db.getValue(v) resultFuture.flatMap { ve ...
  • 我相信你得到的答案不必要地混合在一起,当Future已经完全有能力传达失败时,他们会混合在一起。 您遗漏的主要内容是从Option到选项的值而不显式抛出异常的方法。 我建议您将Failures对象更改为以下内容: object Failures { sealed trait Failure extends Exception // Four types of possible failures here case object UserNotFound extends Failure ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。