Example #1
0
func (a *App) getLdapConfig(rw http.ResponseWriter, req *http.Request) {
	ctxt, err := GetContext(req)
	if err != nil {
		logger.Get().Error("Error Getting the context. error: %v", err)
	}
	ldapConfig, err := GetAuthProvider().GetDirectory()
	if err != nil {
		logger.Get().Error("%s-Unable to reterive directory service configuration:%s", ctxt, err)
		HandleHttpError(rw, err)
		return
	}

	json.NewEncoder(rw).Encode(
		struct {
			LdapServer  string `json:"ldapserver"`
			Port        uint   `json:"port"`
			Base        string `json:"base"`
			Type        string `json:"ldaptype"`
			DomainAdmin string `json:"domainadmin"`
			Password    string `json:"password"`
			Uid         string `json:"uid"`
			FirstName   string `json:"firstname"`
			LastName    string `json:"lastname"`
			DisplayName string `json:"displayname"`
			Email       string `json:"email"`
		}{ldapConfig.LdapServer, ldapConfig.Port, ldapConfig.Base, ldapConfig.Type,
			ldapConfig.DomainAdmin, "", ldapConfig.Uid, ldapConfig.FirstName,
			ldapConfig.LastName, ldapConfig.DisplayName, ldapConfig.Email})
}
Example #2
0
func CreateDefaultECProfiles(ctxt string, mon string, clusterId uuid.UUID) (bool, error) {
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	var cluster models.Cluster
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_CLUSTERS)
	if err := coll.Find(bson.M{"clusterid": clusterId}).One(&cluster); err != nil {
		logger.Get().Error("%s-Error getting cluster details for %v. error: %v", ctxt, clusterId, err)
		return false, err
	}
	var cmdMap map[string]string = map[string]string{
		"4+2": fmt.Sprintf("ceph osd erasure-code-profile set k4m2 plugin=jerasure k=4 m=2 --cluster %s", cluster.Name),
		"6+3": fmt.Sprintf("ceph osd erasure-code-profile set k6m3 plugin=jerasure k=6 m=3 --cluster %s", cluster.Name),
		"8+4": fmt.Sprintf("ceph osd erasure-code-profile set k8m4 plugin=jerasure k=8 m=4 --cluster %s", cluster.Name),
	}

	for k, v := range cmdMap {
		ok, _, err := cephapi_backend.ExecCmd(mon, clusterId, v, ctxt)
		if err != nil || !ok {
			logger.Get().Error("%s-Error creating EC profile for %s. error: %v", ctxt, k, err)
			continue
		} else {
			logger.Get().Debug("%s-Added EC profile for %s", ctxt, k)
		}
	}
	return true, nil
}
Example #3
0
func (a *App) getUser(rw http.ResponseWriter, req *http.Request) {
	ctxt, err := GetContext(req)
	if err != nil {
		logger.Get().Error("Error Getting the context. error: %v", err)
	}

	vars := mux.Vars(req)
	user, err := GetAuthProvider().GetUser(vars["username"], req)
	if err != nil {
		rw.WriteHeader(http.StatusNotFound)
		bytes, _ := json.Marshal(apiError{Error: err.Error()})
		rw.Write(bytes)
		logger.Get().Error("%s-Unable to Get the user:%s", ctxt, err)
		return
	}

	//hide the hash field
	bytes, err := json.Marshal(struct {
		*models.User
		Hash bool `json:"hash,omitempty"`
	}{
		User: &user,
	})
	if err != nil {
		logger.Get().Error("%s-Unable to marshal the User:%s", ctxt, err)
		HandleHttpError(rw, err)
		return
	}
	rw.Write(bytes)

}
Example #4
0
func (a *App) About(w http.ResponseWriter, r *http.Request) {
	ctxt, err := GetContext(r)
	if err != nil {
		logger.Get().Error("Error Getting the context. error: %v", err)
	}
	var syscapabilities conf.SystemCapabilities
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_SYSTEM_CAPABILITIES)
	if err := coll.Find(nil).One(&syscapabilities); err != nil {
		HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error in retrieving System Capabilities detail. error: %v", err))
		logger.Get().Error("%s-Error in retrieving System Capabilities detail. error: %v", ctxt, err)
		return
	}
	storage_provider_details := make(map[string]string)
	for key, val := range syscapabilities.StorageProviderDetails {
		storage_provider_details[strings.Title(key)] = val
		delete(syscapabilities.StorageProviderDetails, key)
	}
	syscapabilities.StorageProviderDetails = storage_provider_details
	if err := json.NewEncoder(w).Encode(syscapabilities); err != nil {
		logger.Get().Error("%s-Error encoding data: %v", ctxt, err)
		HttpResponse(w, http.StatusInternalServerError, err.Error())
	}
}
Example #5
0
func (a *App) FetchClusterDetailsFromProvider(ctxt string, clusterId uuid.UUID) (retVal map[string]map[string]interface{}, err error) {
	retVal = make(map[string]map[string]interface{})
	var result models.RpcResponse
	vars := make(map[string]string)
	vars["cluster-id"] = clusterId.String()
	provider := a.GetProviderFromClusterId(ctxt, clusterId)
	if provider == nil {
		logger.Get().Error("%s-Faield to get provider for cluster: %v", ctxt, clusterId)
		return nil, fmt.Errorf("Faield to get provider for cluster: %v", clusterId)
	}
	err = provider.Client.Call(fmt.Sprintf("%s.%s", provider.Name, "GetClusterSummary"), models.RpcRequest{RpcRequestVars: vars, RpcRequestData: []byte{}, RpcRequestContext: ctxt}, &result)
	if result.Status.StatusCode == http.StatusOK || result.Status.StatusCode == http.StatusPartialContent {
		providerResult := make(map[string]interface{})
		unmarshalError := json.Unmarshal(result.Data.Result, &providerResult)
		if unmarshalError != nil {
			logger.Get().Error("%s - Error unmarshalling the monitoring data from provider %v.Error %v", ctxt, provider.Name, unmarshalError.Error())
			return nil, fmt.Errorf("%s - Error unmarshalling the monitoring data from provider %v.Error %v", ctxt, provider.Name, unmarshalError.Error())
		}
		retVal[provider.Name] = providerResult
	}
	if err != nil {
		return retVal, fmt.Errorf("%s - Call to provider %v failed.Err: %v\n", ctxt, provider.Name, err)
	}
	if result.Status.StatusMessage != "" && (result.Status.StatusCode == http.StatusPartialContent || result.Status.StatusCode == http.StatusInternalServerError || result.Status.StatusCode == http.StatusBadRequest) {
		return retVal, fmt.Errorf("%s - Call to provider %v failed.Err: %v\n", ctxt, provider.Name, result.Status.StatusMessage)
	}
	return retVal, nil
}
Example #6
0
func GetEventById(w http.ResponseWriter, r *http.Request) {
	ctxt, err := GetContext(r)
	if err != nil {
		logger.Get().Error("Error Getting the context. error: %v", err)
	}

	vars := mux.Vars(r)
	event_id_str := vars["event-id"]
	event_id, err := uuid.Parse(event_id_str)
	if err != nil {
		logger.Get().Error("%s-Error parsing event id: %s", ctxt, event_id_str)
		HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing event id: %s", event_id_str))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_APP_EVENTS)
	var event models.AppEvent
	if err := collection.Find(bson.M{"eventid": *event_id}).One(&event); err == mgo.ErrNotFound {
		HttpResponse(w, http.StatusNotFound, "Event not found")
		logger.Get().Error("%s-Event: %v not found. error: %v", ctxt, *event_id, err)
		return
	} else if err != nil {
		logger.Get().Error("%s-Error getting the event detail for %v. error: %v", ctxt, *event_id, err)
		HttpResponse(w, http.StatusBadRequest, "Event finding the record")
		return
	} else {
		json.NewEncoder(w).Encode(event)
	}
}
Example #7
0
//Middleware to create the logging context
func (a *App) LoggingContext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	session, err := Store.Get(r, "session-key")
	if err != nil {
		logger.Get().Error("Error Getting the session. error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	var username string
	if val, ok := session.Values["username"]; ok {
		username = val.(string)
	}

	reqId, err := uuid.New()
	if err != nil {
		logger.Get().Error("Error Creating the RequestId. error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	loggingContext := fmt.Sprintf("%v:%v", username, reqId.String())

	context.Set(r, LoggingCtxt, loggingContext)

	defer context.Clear(r)
	next(w, r)
}
Example #8
0
func checkAndUpdateAutoExpand(t *task.Task, clusterId uuid.UUID, ctxt string) {
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_LOGICAL_UNITS)
	var slus []models.StorageLogicalUnit
	if err := coll.Find(bson.M{"clusterid": clusterId}).All(&slus); err != nil {
		logger.Get().Error(
			"%s-Error getting SLUs of cluster: %v for colocation check. error: %v",
			ctxt,
			clusterId,
			err)
		return
	}

	for _, slu := range slus {
		journalDet := slu.Options["journal"].(map[string]interface{})
		if slu.Options["device"] == journalDet["journaldisk"].(string) {
			coll1 := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_CLUSTERS)
			if err := coll1.Update(
				bson.M{"clusterid": clusterId},
				bson.M{"$set": bson.M{"autoexpand": false}}); err != nil {
				logger.Get().Error(
					"%s-Error setting autoexpand flag for cluster: %v. error: %v",
					ctxt,
					clusterId,
					err)
				return
			}
			t.UpdateStatus("Disabled auto expand for cluster")
			break
		}
	}
	return
}
Example #9
0
func NewLdapAuthProvider(config io.Reader) (*Authorizer, error) {
	if config == nil {
		errStr := "missing configuration file for Ldap Auth provider"
		logger.Get().Error(errStr)
		return nil, fmt.Errorf(errStr)
	}

	userDao := skyring.GetDbProvider().UserInterface()

	session := db.GetDatastore()
	c := session.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_LDAP)
	ldapDao := models.Directory{}
	err := c.Find(bson.M{}).One(&ldapDao)
	if err != nil {
		logger.Get().Info("Failed to open ldap db collection:%s", err)
	}
	//Create the Provider
	if provider, err := NewAuthorizer(userDao, ldapDao); err != nil {
		logger.Get().Error("Unable to initialize the authorizer for Ldapauthprovider. error: %v", err)
		panic(err)
	} else {
		return &provider, nil
	}

}
Example #10
0
func SyncNodeUtilizations(params map[string]interface{}) {
	ctxt, ctxtOk := params["ctxt"].(string)
	if !ctxtOk {
		logger.Get().Error("Failed to fetch context")
		return
	}
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	var nodes []models.Node
	if err := coll.Find(bson.M{"state": models.NODE_STATE_ACTIVE}).All(&nodes); err != nil {
		if err == mgo.ErrNotFound {
			return
		}
		logger.Get().Warning("%s - Failed to fetch nodes in active state.Error %v", ctxt, err)
	}
	time_stamp_str := strconv.FormatInt(time.Now().Unix(), 10)

	var nodeNames []string
	for _, node := range nodes {
		if node.State == models.NODE_STATE_ACTIVE {
			nodeNames = append(nodeNames, node.Hostname)
		}
	}

	if len(nodeNames) == 0 {
		return
	}

	for _, node := range nodes {
		go SyncNodeUtilization(ctxt, node, time_stamp_str)
	}
	go ComputeSystemSummary(make(map[string]interface{}))
}
Example #11
0
func UpdateAlarmCount(event models.AppEvent, severity models.AlarmStatus, ctxt string) error {
	if presentInList(ClusterAffectingEntities, event.NotificationEntity) {
		if err := UpdateClusterAlarmCount(event, severity, ctxt); err != nil {
			logger.Get().Error("%s-Could not update Alarm count for Cluster, for alert: %v. Error: %v", ctxt, event.EventId, err)
		}
	}
	if presentInList(HostAffectingEntities, event.NotificationEntity) {
		if err := UpdateNodeAlarmCount(event, severity, ctxt); err != nil {
			logger.Get().Error("%s-Could not update Alarm count for node, for alert: %v. Error: %v", ctxt, event.EventId, err)
		}
	}
	if presentInList(SluAffectingEntities, event.NotificationEntity) {
		if err := UpdateSluAlarmCount(event, severity, ctxt); err != nil {
			logger.Get().Error("%s-Could not update Alarm count for Slu, for alert: %v. Error: %v", ctxt, event.EventId, err)
		}
	}
	if presentInList(StorageAffectingEntities, event.NotificationEntity) {
		if err := UpdateStorageAlarmCount(event, severity, ctxt); err != nil {
			logger.Get().Error("%s-Could not update Alarm count for storage, for alert: %v. Error: %v", ctxt, event.EventId, err)
		}
	}
	if presentInList(BlockDeviceAffectingEntities, event.NotificationEntity) {
		if err := UpdateBlockDeviceAlarmCount(event, severity, ctxt); err != nil {
			logger.Get().Error("%s-Could not update Alarm count for Block Device, for alert: %v. Error: %v", ctxt, event.EventId, err)
		}
	}
	return nil
}
Example #12
0
func AuditLog(ctxt string, event models.AppEvent, dbprovider dbprovider.DbInterface) error {
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	if event.ClusterName == "" {
		coll := sessionCopy.DB(
			conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_CLUSTERS)
		var cluster models.Cluster
		if err := coll.Find(bson.M{"clusterid": event.ClusterId}).One(&cluster); err == nil {
			event.ClusterName = cluster.Name
		}
	}
	event.Context = ctxt
	if event.Notify {
		subject, body, err := getMailDetails(event)
		if err != nil {
			logger.Get().Error("%s-Could not get mail details for event: %s", ctxt, event.Name)
		} else {
			if err = notifier.MailNotify(subject, body, dbprovider, ctxt); err == nil {
				event.Notified = true
			} else {
				logger.Get().Error("%s-Could not send mail for event: %s", ctxt, event.Name)
			}
		}
	}

	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_APP_EVENTS)
	if err := coll.Insert(event); err != nil {
		logger.Get().Error("%s-Error adding the app event: %v", ctxt, err)
		return err
	}
	return nil
}
Example #13
0
func node_lost_handler(event models.AppEvent, ctxt string) (models.AppEvent, error) {
	//worry about the node only if the node in active state
	state, err := skyringutils.GetNodeStateById(ctxt, event.NodeId)
	if state == models.NODE_STATE_ACTIVE && err == nil {
		if err := skyringutils.Update_node_status_byId(ctxt, event.NodeId, models.NODE_STATUS_ERROR); err != nil {
			return event, err
		}
	}
	event.Name = skyring.EventTypes["NODE_STATE_CHANGED"]
	event.Message = fmt.Sprintf("Host: %s lost contact", event.NodeName)
	event.EntityId = event.NodeId
	event.Severity = models.ALARM_STATUS_MAJOR
	event.NotificationEntity = models.NOTIFICATION_ENTITY_HOST
	event.Notify = true

	util.AppendServiceToNode(bson.M{"nodeid": event.NodeId}, models.SkyringServices[1], models.STATUS_DOWN, ctxt)

	if err := update_alarm_count(event, ctxt); err != nil {
		logger.Get().Error("%s-could not update alarm"+
			" count for event: %s", ctxt, event.EventId.String())
		return event, err
	}
	app := skyring.GetApp()
	if err := app.RouteProviderEvents(ctxt, event); err != nil {
		logger.Get().Error("%s-Event:%s could not be handled for node: %s. error: %v", ctxt, event.Name, event.NodeName, err)
	}
	return event, nil
}
Example #14
0
func TestMailNotify(notifier models.MailNotifier, subject string, body string, recepient []string, ctxt string) error {
	subject = fmt.Sprintf("%s: ", notifier.SubPrefix) + subject
	msg := []byte("To: " + recepient[0] + "\r\n" +
		"Subject: " + subject + "\r\n" +
		"\r\n" +
		body + "\r\n")
	err := SetMailClient(notifier, ctxt)
	if err != nil {
		logger.Get().Error("%s-Error setting the Mail Client Error: %v", ctxt, err)
		return err
	}

	err = sendMail(notifier.MailId, recepient, msg)
	if err != nil {
		// retry once again after setting the client, as client might have timed out
		if err := SetMailClient(notifier, ctxt); err != nil {
			if err != nil {
				logger.Get().Error("%s-Error setting the Mail Client Error: %v", ctxt, err)
				return err
			}
		}
		if err := sendMail(notifier.MailId, recepient, msg); err != nil {
			logger.Get().Error("%s-Could not Send the Mail Notification. Error: %v", ctxt, err)
			return err
		}
	}
	client = nil
	return nil
}
Example #15
0
// InitPlugin creates an instance of the named plugin.
func InitAuthProvider(name string, configFilePath string) (AuthInterface, error) {
	var authprovider AuthInterface

	if name == "" {
		logger.Get().Info("No providers specified.")
		return nil, nil
	}

	var err error

	if configFilePath != "" {
		config, err := os.Open(configFilePath)
		if err != nil {
			logger.Get().Critical("Couldn't open auth provider configuration %s. error: %v",
				configFilePath, err)
		}

		defer config.Close()
		authprovider, err = GetAuthProvider(name, config)
	} else {
		// Pass explicit nil so providers can actually check for nil. See
		// "Why is my nil error value not equal to nil?" in golang.org/doc/faq.
		authprovider, err = GetAuthProvider(name, nil)
	}

	if err != nil {
		return nil, fmt.Errorf("could not init plugin %s. error: %v", name, err)
	}
	if authprovider == nil {
		return nil, fmt.Errorf("unknown plugin %s", name)
	}

	return authprovider, nil
}
Example #16
0
func LoadAppConfiguration(configFilePath string, configFile string) {
	file, err := ioutil.ReadFile(path.Join(configFilePath, configFile))
	if err != nil {
		logger.Get().Critical("Error reading skyring config. error: %v", err)
		return
	}
	err = json.Unmarshal(file, &SystemConfig)
	if err != nil {
		logger.Get().Critical("Error unmarshalling skyring config. error: %v", err)
		return
	}
	//Initialize the Provisioner Map
	SystemConfig.Provisioners = make(map[string]ProvisionerConfig)
	//Initialize System_capabilities
	file, err = ioutil.ReadFile(path.Join(configFilePath, aboutConfigFile))
	if err != nil {
		logger.Get().Critical("Error reading about config. error: %v", err)
		return
	}
	err = json.Unmarshal(file, &SystemConfig.SysCapabilities)
	if err != nil {
		logger.Get().Critical("Error unmarshalling about config. error: %v", err)
		return
	}
	SystemConfig.SysCapabilities.StorageProviderDetails = make(map[string]string)
}
Example #17
0
func (a *App) GET_ClusterBlockDevices(w http.ResponseWriter, r *http.Request) {
	ctxt, err := GetContext(r)
	if err != nil {
		logger.Get().Error("Error Getting the context. error: %v", err)
	}

	vars := mux.Vars(r)
	cluster_id_str := vars["cluster-id"]
	cluster_id, err := uuid.Parse(cluster_id_str)
	if err != nil {
		logger.Get().Error("%s - Error parsing the cluster id: %s. error: %v", ctxt, cluster_id_str, err)
		HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing the cluster id: %s", cluster_id_str))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_BLOCK_DEVICES)
	var blkDevices []models.BlockDevice
	if err := collection.Find(bson.M{"clusterid": *cluster_id}).All(&blkDevices); err != nil {
		if err == mgo.ErrNotFound {
			HttpResponse(w, http.StatusNotFound, err.Error())
		} else {
			HttpResponse(w, http.StatusInternalServerError, err.Error())
		}
		logger.Get().Error("%s - Error getting the block devices list for cluster: %v. error: %v", ctxt, *cluster_id, err)
		return
	}
	if len(blkDevices) == 0 {
		json.NewEncoder(w).Encode([]models.BlockDevice{})
	} else {
		json.NewEncoder(w).Encode(blkDevices)
	}
}
Example #18
0
// CurrentUser returns the currently logged in user and a boolean validating
// the information.
func (a Authorizer) GetUser(u string, req *http.Request) (user models.User, e error) {
	// This should search and fetch the user name based on the given value
	// and can also check whether the available users are already imported
	// into the database or not.

	//if username is me, get the currently loggedin user
	if u == authprovider.CurrentUser {
		session, err := skyring.Store.Get(req, "session-key")
		if err != nil {
			logger.Get().Error("Error getting the session for user: %s. error: %v", u, err)
			return user, err
		}
		if val, ok := session.Values["username"]; ok {
			u = val.(string)
		} else {
			logger.Get().Error("Unable to identify the user: %s from session", u)
			return user, mkerror("Unable to identify the user from session")
		}
	}
	user, e = a.userDao.User(u)
	if e != nil {
		logger.Get().Error("Error retrieving the user: %s. error: %v", user, e)
		return user, e
	}
	return user, nil
}
Example #19
0
func NewLocalAuthProvider(config io.Reader) (*Authorizer, error) {
	if config == nil {
		logger.Get().Error("missing configuration file for Local Auth provider")
		return nil, fmt.Errorf("missing configuration file for Local Auth provider")
	}

	providerCfg := LocalProviderCfg{}

	bytes, err := ioutil.ReadAll(config)
	if err != nil {
		logger.Get().Error("Error reading configuration file. error: %v", err)
		return nil, err
	}
	if err = json.Unmarshal(bytes, &providerCfg); err != nil {
		logger.Get().Error("Unable to Unmarshall the data. error: %v", err)
		return nil, err
	}
	//Create DB Backend
	userDao := skyring.GetDbProvider().UserInterface()

	//Create the Provider
	if provider, err := NewAuthorizer(userDao, providerCfg.DefaultRole, providerCfg.Roles); err != nil {
		logger.Get().Error("Unable to initialize the authorizer for localauthprovider. error: %v", err)
		panic(err)
	} else {
		return &provider, nil
	}
}
Example #20
0
func sync_cluster_status(ctxt string, cluster models.Cluster, provider *Provider) (bool, error) {
	var result models.RpcResponse
	vars := make(map[string]string)
	vars["cluster-id"] = cluster.ClusterId.String()
	err = provider.Client.Call(
		fmt.Sprintf("%s.%s", provider.Name, sync_functions["cluster_status"]),
		models.RpcRequest{RpcRequestVars: vars, RpcRequestData: []byte{}, RpcRequestContext: ctxt},
		&result)
	if err != nil || result.Status.StatusCode != http.StatusOK {
		logger.Get().Error("%s-Error getting status for cluster: %s. error:%v", ctxt, cluster.Name, err)
		return false, err
	}
	clusterStatus, err := strconv.Atoi(string(result.Data.Result))
	if err != nil {
		logger.Get().Error("%s-Error getting status for cluster: %s. error:%v", ctxt, cluster.Name, err)
		return false, err
	}

	// Set the cluster status
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_CLUSTERS)
	logger.Get().Info("%s-Updating the status of the cluster: %s to %d", ctxt, cluster.Name, clusterStatus)
	if err := coll.Update(bson.M{"clusterid": cluster.ClusterId}, bson.M{"$set": bson.M{"status": clusterStatus}}); err != nil {
		logger.Get().Error("%s-Error updating status for cluster: %s. error:%v", ctxt, cluster.Name, err)
		return false, err
	}

	return true, nil
}
Example #21
0
func update_alarm_count(event models.AppEvent, ctxt string) error {
	clearedSeverity, err := common_event.ClearCorrespondingAlert(event, ctxt)
	if err != nil && event.Severity == models.ALARM_STATUS_CLEARED {
		logger.Get().Warning("%s-could not clear corresponding alert for: %s. Error: %v", ctxt, event.EventId.String(), err)
		return err
	}

	if err = common_event.UpdateNodeAlarmCount(
		event,
		clearedSeverity,
		ctxt); err != nil {
		logger.Get().Error("%s-Could not update Alarm state and"+
			" count for event:%v .Error: %v", ctxt, event.EventId, err)
		return err
	}

	if !event.ClusterId.IsZero() {
		if err = common_event.UpdateClusterAlarmCount(
			event, clearedSeverity, ctxt); err != nil {
			logger.Get().Error("%s-Could not update Alarm state and count for"+
				" event:%v .Error: %v", ctxt, event.EventId, err)
			return err
		}
	}
	return nil
}
Example #22
0
func (a *App) getSubTasks(rw http.ResponseWriter, req *http.Request) {
	ctxt, err := GetContext(req)
	if err != nil {
		logger.Get().Error("Error Getting the context. error: %v", err)
	}
	vars := mux.Vars(req)
	taskId, err := uuid.Parse(vars["taskid"])
	if err != nil {
		logger.Get().Error("%s-Unable to Parse the Id: %s. error: %v", ctxt, vars["taskId"], err)
		rw.WriteHeader(http.StatusBadRequest)
		bytes, _ := json.Marshal(APIError{Error: err.Error()})
		rw.Write(bytes)
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_TASKS)
	var tasks []models.AppTask
	if err := coll.Find(bson.M{"parentid": *taskId}).All(&tasks); err != nil {
		logger.Get().Error("%s-Unable to get tasks. error: %v", ctxt, err)
		if err == mgo.ErrNotFound {
			HttpResponse(rw, http.StatusNotFound, err.Error())
		} else {
			HttpResponse(rw, http.StatusInternalServerError, err.Error())
		}
		return
	}
	if len(tasks) == 0 {
		json.NewEncoder(rw).Encode([]models.AppTask{})
	} else {
		json.NewEncoder(rw).Encode(tasks)
	}
}
Example #23
0
func network_utilization(ctxt string, node *models.Node, time_stamp_str string) {
	prevUtilization := node.Utilizations["networkusage"]
	nwFetchTimeStamp := prevUtilization.UpdatedAt
	var nwUsed float64
	nwUsedFetchSuccess := true
	resource_name, resourceNameError := GetMonitoringManager().GetResourceName(map[string]interface{}{"resource_name": monitoring.AVERAGE + monitoring.INTERFACE + monitoring.USED})
	if resourceNameError != nil {
		logger.Get().Warning("%s - Failed to fetch resource name of %v for %v .Err %v", ctxt, monitoring.AVERAGE+monitoring.INTERFACE+monitoring.USED, node.Hostname, resourceNameError)
		nwUsedFetchSuccess = false
		nwUsed = float64(prevUtilization.Used)
	} else {
		nwUsed, nwUsedFetchSuccess = FetchStatFromGraphiteWithErrorIndicate(ctxt, node.Hostname, resource_name)
		if !nwUsedFetchSuccess {
			nwUsedFetchSuccess = false
			nwUsed = float64(prevUtilization.Used)
		}
	}

	var nwBandwidth float64
	nwBandwidthSuccess := true
	resource_name, resourceNameError = GetMonitoringManager().GetResourceName(map[string]interface{}{"resource_name": monitoring.AVERAGE + monitoring.INTERFACE + monitoring.TOTAL})
	if resourceNameError != nil {
		logger.Get().Warning("%s - Failed to fetch resource name of %v for %v .Err %v", ctxt, monitoring.AVERAGE+monitoring.INTERFACE+monitoring.TOTAL, node.Hostname, resourceNameError)
		nwBandwidthSuccess = false
		nwBandwidth = float64(prevUtilization.Total)
	} else {
		nwBandwidth, nwBandwidthSuccess = FetchStatFromGraphiteWithErrorIndicate(ctxt, node.Hostname, resource_name)
		if !nwBandwidthSuccess {
			nwBandwidthSuccess = false
			nwBandwidth = float64(prevUtilization.Total)
		}
	}

	var nwPercentUsage float64
	nwPercentSuccess := true
	resource_name, resourceNameError = GetMonitoringManager().GetResourceName(map[string]interface{}{"resource_name": monitoring.AVERAGE + monitoring.INTERFACE + monitoring.PERCENT})
	if resourceNameError != nil {
		logger.Get().Warning("%s - Failed to fetch resource name of %v for %v .Err %v", ctxt, monitoring.AVERAGE+monitoring.INTERFACE+monitoring.PERCENT, node.Hostname, resourceNameError)
		nwPercentSuccess = false
		nwPercentUsage = prevUtilization.PercentUsed
	} else {
		nwPercentUsage, nwPercentSuccess = FetchStatFromGraphiteWithErrorIndicate(ctxt, node.Hostname, resource_name)
		if !nwPercentSuccess {
			nwPercentSuccess = false
			nwPercentUsage = prevUtilization.PercentUsed
		}
	}

	if nwPercentSuccess && nwBandwidthSuccess && nwUsedFetchSuccess {
		nwFetchTimeStamp = time.Now().String()
		node.Utilizations["networkusage"] = models.Utilization{
			Used:        int64(nwUsed),
			Total:       int64(nwBandwidth),
			PercentUsed: nwPercentUsage,
			UpdatedAt:   nwFetchTimeStamp,
		}
	}
}
Example #24
0
func check_task_status(params map[string]interface{}) {
	reqId, err := uuid.New()
	if err != nil {
		logger.Get().Error("Error Creating the RequestId. error: %v", err)
		return
	}
	ctxt := fmt.Sprintf("%v:%v", models.ENGINE_NAME, reqId.String())
	var id uuid.UUID
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	var tasks []models.AppTask
	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_TASKS)
	if err := collection.Find(bson.M{"completed": false, "parentid": id}).All(&tasks); err != nil {
		logger.Get().Warning("%s-%v", ctxt, err.Error())
		return
	}
	application := GetApp()
	for _, task := range tasks {
		if ok := check_TaskLastUpdate(task); ok {
			// Fetch the child tasks for this task id
			var subTasks []models.AppTask
			if err := collection.Find(bson.M{"completed": false, "parentid": task.Id}).All(&subTasks); err != nil && err != mgo.ErrNotFound {
				logger.Get().Warning("%s-%v", ctxt, err.Error())
				return
			}
			if len(subTasks) == 0 {
				//Stopping parent task
				stop_ParentTask(task, ctxt)
			} else {
				var result models.RpcResponse
				var stoppedSubTasksCount = 0
				for _, subTask := range subTasks {
					if ok := check_TaskLastUpdate(subTask); ok {
						provider := application.getProviderFromClusterType(subTask.Owner)
						vars := make(map[string]string)
						vars["task-id"] = subTask.Id.String()
						err := provider.Client.Call(fmt.Sprintf("%s.%s",
							provider.Name, "StopTask"),
							models.RpcRequest{RpcRequestVars: vars, RpcRequestData: []byte{}, RpcRequestContext: ctxt},
							&result)
						if err != nil || (result.Status.StatusCode != http.StatusOK) {
							logger.Get().Warning(fmt.Sprintf(":%s-Error stopping sub task: %v. error:%v", ctxt, subTask.Id, err))
							continue
						}
						stoppedSubTasksCount++
					}
				}
				if stoppedSubTasksCount == len(subTasks) {
					// Stopping parent task
					stop_ParentTask(task, ctxt)
				}
			}
		}
	}
}
Example #25
0
func drive_remove_handler(event models.AppEvent, ctxt string) (models.AppEvent, error) {
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	var node models.Node
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	if err := coll.Find(bson.M{"nodeid": event.NodeId}).One(&node); err != nil {
		logger.Get().Error("%s-Node information read from DB failed for node: %s. error: %v", ctxt, event.NodeId, err)
		return event, err
	}

	sProfiles, err := skyring.GetDbProvider().StorageProfileInterface().StorageProfiles(ctxt, nil, models.QueryOps{})
	if err != nil {
		logger.Get().Error("%s-Unable to get the storage profiles. err:%v", ctxt, err)
		return event, err
	}

	previous_disks := node.StorageDisks
	// sync the nodes to get new disks
	if ok, err := skyring.GetCoreNodeManager().SyncStorageDisks(node.Hostname, sProfiles, ctxt); err != nil || !ok {
		logger.Get().Error("%s-Failed to sync disk for host: %s Error: %v", ctxt, node.Hostname, err)
		return event, err
	}

	if err := coll.Find(bson.M{"nodeid": event.NodeId}).One(&node); err != nil {
		logger.Get().Error("%s-Node information read from DB failed for node: %s. error: %v", ctxt, event.NodeId, err)
		return event, err
	}

	var exists bool
	for _, disk := range previous_disks {
		exists = false
		for _, new_disk := range node.StorageDisks {
			if disk.DevName == new_disk.DevName {
				exists = true
				break
			}
		}
		if !exists && !disk.Used {
			event.EntityId = disk.DiskId
			event.Tags["DevName"] = disk.DevName
			event.Tags["DriveSize"] = strconv.FormatFloat(disk.Size, 'E', -1, 64)
			event.Tags["Type"] = disk.Type

		}
	}

	// adding the details for event

	event.Name = skyring.EventTypes["DRIVE_REMOVE"]
	event.NotificationEntity = models.NOTIFICATION_ENTITY_HOST
	event.Message = fmt.Sprintf("Storage Drive: %s removed from Host:%s", event.Tags["DevName"], node.Hostname)
	event.Severity = models.ALARM_STATUS_CLEARED
	event.Notify = false
	return event, nil
}
Example #26
0
func (a *App) LoadEventTypes(configDir string, configFile string) error {
	var coreEventTypes = make(map[string]string)
	var providerEventTypes = make(map[string]string)
	file, err := ioutil.ReadFile(path.Join(configDir, configFile))
	if err != nil {
		return fmt.Errorf(
			"Error reading event types from %s",
			path.Join(configDir, configFile))
	}
	err = json.Unmarshal(file, &coreEventTypes)
	if err != nil {
		return fmt.Errorf(
			"Error un-marshalling core event types. error: %v",
			err)
	}

	files, err := ioutil.ReadDir(path.Join(configDir, conf.ProviderConfDir))
	if err != nil {
		return fmt.Errorf(
			"Error reading provider specific events. error: %v",
			err)
	}
	for _, f := range files {
		if strings.HasSuffix(f.Name(), ".evt") {
			file, err := ioutil.ReadFile(path.Join(
				configDir,
				conf.ProviderConfDir,
				f.Name()))
			if err != nil {
				logger.Get().Critical(
					"Error reading events list from %s",
					f.Name())
				continue
			}
			err = json.Unmarshal(file, &providerEventTypes)
			if err != nil {
				logger.Get().Critical(
					"Error unmarshalling event types from %s",
					f.Name())
				continue
			}
			for key, value := range providerEventTypes {
				if _, ok := coreEventTypes[key]; ok {
					return fmt.Errorf(
						"Duplicate event type: %s found",
						key)
				}
				coreEventTypes[key] = value
			}
		}
	}
	EventTypes = coreEventTypes
	return nil
}
Example #27
0
func (a *App) SetRoutes(router *mux.Router) error {
	// Load App specific routes
	a.LoadRoutes()

	// Create a router for defining the routes which require authenticationAuth_fix
	//For routes require auth, will be checked by a middleware which
	//return error immediately

	authReqdRouter := mux.NewRouter().StrictSlash(true)

	//create a negroni with LoginReqd middleware
	n := negroni.New(negroni.HandlerFunc(a.LoginRequired), negroni.Wrap(authReqdRouter))

	// Set routes for core which doesnot require authentication
	for _, route := range CORE_ROUTES_NOAUTH {
		if validApiVersion(route.Version) {
			urlPattern := fmt.Sprintf("%s/v%d/%s", DEFAULT_API_PREFIX, route.Version, route.Pattern)
			router.Methods(route.Method).Path(urlPattern).Name(route.Name).Handler(http.HandlerFunc(route.HandlerFunc))
		} else {
			logger.Get().Info("Skipped the route: %s as version: %d is un spported", route.Name, route.Version)
		}
	}

	// Set routes for core which require authentication
	for _, route := range CORE_ROUTES {
		if validApiVersion(route.Version) {
			urlPattern := fmt.Sprintf("%s/v%d/%s", DEFAULT_API_PREFIX, route.Version, route.Pattern)
			authReqdRouter.Methods(route.Method).Path(urlPattern).Name(route.Name).Handler(http.HandlerFunc(route.HandlerFunc))
			router.Handle(urlPattern, n)
		} else {
			logger.Get().Info("Skipped the route: %s as version: %d is un spported", route.Name, route.Version)
		}
	}

	//Set the provider specific routes here
	//All the provider specific routes are assumed to be authenticated
	for _, route := range a.routes {
		logger.Get().Debug("%s", route)
		if validApiVersion(route.Version) {
			urlPattern := fmt.Sprintf("%s/v%d/%s", DEFAULT_API_PREFIX, route.Version, route.Pattern)
			authReqdRouter.
				Methods(route.Method).
				Path(urlPattern).
				Name(route.Name).
				Handler(http.HandlerFunc(a.ProviderHandler))
			router.Handle(urlPattern, n)
		} else {
			logger.Get().Info("Skipped the route: %s as version: %d is un spported", route.Name, route.Version)
		}
	}

	return nil
}
Example #28
0
func InitializeDb() error {
	if mgr, err := dbprovider.InitDbProvider("mongodbprovider", ""); err != nil {
		logger.Get().Error("Error Initializing the Db Provider: %s", err)
		return err
	} else {
		DbManager = mgr
	}
	if err := DbManager.InitDb(); err != nil {
		logger.Get().Error("Error Initializing the DAOs. Error: %s", err)
		return err
	}
	return nil
}
Example #29
0
// Logout clears an authentication session and add a logged out message.
func (a Authorizer) Logout(rw http.ResponseWriter, req *http.Request) error {
	session, err := skyring.Store.Get(req, "session-key")
	if err != nil {
		logger.Get().Error("Error getting the session. error: %v", err)
		return err
	}
	session.Options.MaxAge = -1
	if err = session.Save(req, rw); err != nil {
		logger.Get().Error("Error saving the session. error: %v", err)
		return err
	}
	return nil
}
Example #30
0
func UpdateSluCountToSummaries(ctxt string, cluster models.Cluster) {
	sluCnt, err := ComputeSluStatusWiseCount(bson.M{"clusterid": cluster.ClusterId}, bson.M{"utilizationtype": monitoring.SLU_UTILIZATION, "clusterid": cluster.ClusterId, "thresholdseverity": models.CRITICAL})
	if err != nil {
		logger.Get().Error("%s - Failed to fetch slu status wise count for cluster %v.Error %v", ctxt, cluster.Name, err)
	} else {
		UpdateDb(bson.M{"clusterid": cluster.ClusterId}, bson.M{"slucount": sluCnt}, models.COLL_NAME_CLUSTER_SUMMARY, ctxt)
		sluCnt, err := ComputeSluStatusWiseCount(nil, bson.M{"utilizationtype": monitoring.SLU_UTILIZATION, "thresholdseverity": models.CRITICAL})
		if err != nil {
			logger.Get().Error("%s - Failed to update slu status wise count for system summary.Error %v", ctxt, err)
		} else {
			UpdateDb(bson.M{"name": monitoring.SYSTEM}, bson.M{"slucount": sluCnt}, models.COLL_NAME_SKYRING_UTILIZATION, ctxt)
		}
	}
}