如何使用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
最满意答案
所以现在我已经尝试使用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的同 ...
-
所有Scala期货都已完成(All Scala futures completed)[2022-12-15]
我不确定我是否完全理解你的问题,但我至少可以在第一期提供答案。 在onComplete访问可变状态可能是不安全的。 实际上它会使演员的整个目的无效。 如果您访问可变状态,您应该始终通过actor的邮箱进行操作,例如self ! ModifyState(newValue) self ! ModifyState(newValue) 。 这里的问题是onComplete方法与actor不在同一个线程中,导致可能两个线程同时修改相同数据的情况,因此变异状态具有通常在并发状态变化中存在的所有相同问题(种族)条件等)。 ... -
斯卡拉期货的理解(Scala Futures in For Comprehension)[2024-03-11]
期货开始在这里执行: val fFuture: Future[Int] = Future { println("f called"); 3 } val gFuture: Future[Int] = Future { println("g called"); 4 } 因此两个执 ... -
处理Scala期货(Handling Scala Futures)[2023-08-09]
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 ... -
Scala期货和多线程(Scala Futures and Multithreading)[2023-12-23]
简答:没有。 答案很长: 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 ...
-
在Scala中使用Futures?(Using Futures in Scala?)[2022-10-02]
你必须使用flatMap ,因为如果第一个操作没有返回结果你想要做的事情也会返回一个未来。 这仍然是在编译时尽可能接近您的代码。 请注意,您不能在scala中使用名为val标识符,因为这是一个关键字。 def getValue(v: Foo)(implicit ec: ExecutionContext): Future[Value] = { val resultFuture: Future[Seq[Value2]] = db.getValue(v) resultFuture.flatMap { ve ... -
Scala - 期货和Eithers(Scala - Futures and Eithers)[2022-06-05]
我相信你得到的答案不必要地混合在一起,当Future已经完全有能力传达失败时,他们会混合在一起。 您遗漏的主要内容是从Option到选项的值而不显式抛出异常的方法。 我建议您将Failures对象更改为以下内容: object Failures { sealed trait Failure extends Exception // Four types of possible failures here case object UserNotFound extends Failure ...