Beispiel #1
0
func originFromAPI(apiRec params.LogStreamRecord, controllerUUID string) (logfwd.Origin, error) {
	var origin logfwd.Origin

	tag, err := names.ParseTag(apiRec.Entity)
	if err != nil {
		return origin, errors.Annotate(err, "invalid entity")
	}

	ver, err := version.Parse(apiRec.Version)
	if err != nil {
		return origin, errors.Annotatef(err, "invalid version %q", apiRec.Version)
	}

	switch tag := tag.(type) {
	case names.MachineTag:
		origin = logfwd.OriginForMachineAgent(tag, controllerUUID, apiRec.ModelUUID, ver)
	case names.UnitTag:
		origin = logfwd.OriginForUnitAgent(tag, controllerUUID, apiRec.ModelUUID, ver)
	default:
		origin, err = logfwd.OriginForJuju(tag, controllerUUID, apiRec.ModelUUID, ver)
		if err != nil {
			return origin, errors.Annotate(err, "could not extract origin")
		}
	}
	return origin, nil
}
Beispiel #2
0
func (s *OriginSuite) TestOriginForMachineAgent(c *gc.C) {
	tag := names.NewMachineTag("99")

	origin := logfwd.OriginForMachineAgent(tag, validOrigin.ControllerUUID, validOrigin.ModelUUID, validOrigin.Software.Version)

	c.Check(origin, jc.DeepEquals, logfwd.Origin{
		ControllerUUID: validOrigin.ControllerUUID,
		ModelUUID:      validOrigin.ModelUUID,
		Hostname:       "machine-99." + validOrigin.ModelUUID,
		Type:           logfwd.OriginTypeMachine,
		Name:           "99",
		Software: logfwd.Software{
			PrivateEnterpriseNumber: 28978,
			Name:    "jujud-machine-agent",
			Version: version.MustParse("2.0.1"),
		},
	})
}
Beispiel #3
0
func (s *ClientSuite) TestSendLogLevels(c *gc.C) {
	tag := names.NewMachineTag("99")
	cID := "9f484882-2f18-4fd2-967d-db9663db7bea"
	mID := "deadbeef-2f18-4fd2-967d-db9663db7bea"
	ver := version.MustParse("1.2.3")
	rec := logfwd.Record{
		Origin:    logfwd.OriginForMachineAgent(tag, cID, mID, ver),
		Timestamp: time.Unix(12345, 0),
		Level:     loggo.ERROR,
		Location: logfwd.SourceLocation{
			Module:   "juju.x.y",
			Filename: "x/y/spam.go",
			Line:     42,
		},
		Message: "(╯°□°)╯︵ ┻━┻",
	}
	client := syslog.Client{Sender: s.sender}

	levels := map[loggo.Level]rfc5424.Severity{
		loggo.ERROR:   rfc5424.SeverityError,
		loggo.WARNING: rfc5424.SeverityWarning,
		loggo.INFO:    rfc5424.SeverityInformational,
		loggo.DEBUG:   rfc5424.SeverityDebug,
		loggo.TRACE:   rfc5424.SeverityDebug,
	}
	for level, expected := range levels {
		c.Logf("trying %s -> %s", level, expected)
		s.stub.ResetCalls()
		rec.Level = level

		err := client.Send([]logfwd.Record{rec})
		c.Assert(err, jc.ErrorIsNil)

		msg := s.stub.Calls()[0].Args[0].(rfc5424.Message)
		c.Check(msg.Severity, gc.Equals, expected)
	}
}
Beispiel #4
0
func (s *ClientSuite) TestSendLogFull(c *gc.C) {
	tag := names.NewMachineTag("99")
	cID := "9f484882-2f18-4fd2-967d-db9663db7bea"
	mID := "deadbeef-2f18-4fd2-967d-db9663db7bea"
	ver := version.MustParse("1.2.3")
	ts := time.Unix(12345, 0)
	rec := logfwd.Record{
		Origin:    logfwd.OriginForMachineAgent(tag, cID, mID, ver),
		Timestamp: time.Unix(12345, 0),
		Level:     loggo.ERROR,
		Location: logfwd.SourceLocation{
			Module:   "juju.x.y",
			Filename: "x/y/spam.go",
			Line:     42,
		},
		Message: "(╯°□°)╯︵ ┻━┻",
	}
	client := syslog.Client{Sender: s.sender}

	err := client.Send([]logfwd.Record{rec})
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "Send")
	s.stub.CheckCall(c, 0, "Send", rfc5424.Message{
		Header: rfc5424.Header{
			Priority: rfc5424.Priority{
				Severity: rfc5424.SeverityError,
				Facility: rfc5424.FacilityUser,
			},
			Timestamp: rfc5424.Timestamp{ts},
			Hostname: rfc5424.Hostname{
				FQDN: "machine-99.deadbeef-2f18-4fd2-967d-db9663db7bea",
			},
			AppName: "jujud-machine-agent-deadbeef-2f18-4fd2-967d-db96",
		},
		StructuredData: rfc5424.StructuredData{
			&sdelements.Origin{
				EnterpriseID: sdelements.OriginEnterpriseID{
					Number: 28978,
				},
				SoftwareName:    "jujud-machine-agent",
				SoftwareVersion: ver,
			},
			&sdelements.Private{
				Name: "model",
				PEN:  28978,
				Data: []rfc5424.StructuredDataParam{{
					Name:  "controller-uuid",
					Value: "9f484882-2f18-4fd2-967d-db9663db7bea",
				}, {
					Name:  "model-uuid",
					Value: "deadbeef-2f18-4fd2-967d-db9663db7bea",
				}},
			},
			&sdelements.Private{
				Name: "log",
				PEN:  28978,
				Data: []rfc5424.StructuredDataParam{{
					Name:  "module",
					Value: "juju.x.y",
				}, {
					Name:  "source",
					Value: "x/y/spam.go:42",
				}},
			},
		},
		Msg: "(╯°□°)╯︵ ┻━┻",
	})
}